1/* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "cp-tree.h"
25#include "c-family/c-common.h"
26#include "timevar.h"
27#include "stringpool.h"
28#include "cgraph.h"
29#include "print-tree.h"
30#include "attribs.h"
31#include "trans-mem.h"
32#include "intl.h"
33#include "decl.h"
34#include "c-family/c-objc.h"
35#include "plugin.h"
36#include "tree-pretty-print.h"
37#include "parser.h"
38#include "gomp-constants.h"
39#include "omp-general.h"
40#include "omp-offload.h"
41#include "c-family/c-indentation.h"
42#include "context.h"
43#include "cp-cilkplus.h"
44#include "gcc-rich-location.h"
45#include "tree-iterator.h"
46
47
48/* The lexer. */
49
50/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
52
53static cp_token eof_token =
54{
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
56};
57
58/* The various kinds of non integral constant we encounter. */
59enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
115};
116
117/* The various kinds of errors about name-lookup failing. */
118enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
127};
128
129/* The various kinds of required token */
130enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
152
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
174
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
183};
184
185/* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
187
188class type_id_in_expr_sentinel
189{
190 cp_parser *parser;
191 bool saved;
192public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
199};
200
201/* Prototypes. */
202
203static cp_lexer *cp_lexer_new_main
204 (void);
205static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207static void cp_lexer_destroy
208 (cp_lexer *);
209static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227static void cp_lexer_purge_token
228 (cp_lexer *);
229static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231static void cp_lexer_save_tokens
232 (cp_lexer *);
233static void cp_lexer_commit_tokens
234 (cp_lexer *);
235static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237static void cp_lexer_print_token
238 (FILE *, cp_token *);
239static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245
246static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
248
249static void cp_parser_initial_pragma
250 (cp_token *);
251
252static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
262
263/* Manifest constants. */
264#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265#define CP_SAVED_TOKEN_STACK 5
266
267/* Variables. */
268
269/* The stream to which debugging output should be written. */
270static FILE *cp_lexer_debug_stream;
271
272/* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274int cp_unevaluated_operand;
275
276/* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
281
282static void
283cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
286{
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
290
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
292
293 if (buffer == NULL)
294 return;
295
296 if (num == 0)
297 num = buffer->length ();
298
299 if (start_token == NULL)
300 start_token = buffer->address ();
301
302 if (start_token > buffer->address ())
303 {
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
306 }
307
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
311 {
312 if (token == start_token)
313 do_print = true;
314
315 if (!do_print)
316 continue;
317
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
321
322 cp_lexer_print_token (file, token);
323
324 if (token == curr_token)
325 fprintf (file, "]]");
326
327 switch (token->type)
328 {
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
335
336 default:
337 fputc (' ', file);
338 }
339 }
340
341 if (i == num && i < buffer->length ())
342 {
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
345 }
346
347 fprintf (file, "\n");
348}
349
350
351/* Dump all tokens in BUFFER to stderr. */
352
353void
354cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
355{
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
357}
358
359DEBUG_FUNCTION void
360debug (vec<cp_token, va_gc> &ref)
361{
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
363}
364
365DEBUG_FUNCTION void
366debug (vec<cp_token, va_gc> *ptr)
367{
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
372}
373
374
375/* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
377
378static void
379cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
380{
381 if (t)
382 {
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
385 }
386}
387
388
389/* Dump parser context C to FILE. */
390
391static void
392cp_debug_print_context (FILE *file, cp_parser_context *c)
393{
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
398}
399
400
401/* Print the stack of parsing contexts to FILE starting with FIRST. */
402
403static void
404cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
405{
406 unsigned i;
407 cp_parser_context *c;
408
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
411 {
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
414 }
415}
416
417
418/* Print the value of FLAG to FILE. DESC is a string describing the flag. */
419
420static void
421cp_debug_print_flag (FILE *file, const char *desc, bool flag)
422{
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
425}
426
427
428/* Print an unparsed function entry UF to FILE. */
429
430static void
431cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
432{
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
436
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
441 {
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
447 }
448
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
452 {
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
455 }
456 fprintf (file, "\n");
457
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
461 {
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
464 }
465 fprintf (file, "\n");
466}
467
468
469/* Print the stack of unparsed member functions S to FILE. */
470
471static void
472cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
474{
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
477
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
480 {
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
483 }
484}
485
486
487/* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
489
490static void
491cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
492{
493 cp_token *next_token, *first_token, *start_token;
494
495 if (file == NULL)
496 file = stderr;
497
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
505}
506
507
508/* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
510
511void
512cp_debug_parser (FILE *file, cp_parser *parser)
513{
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
517
518 if (file == NULL)
519 file = stderr;
520
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
594}
595
596DEBUG_FUNCTION void
597debug (cp_parser &ref)
598{
599 cp_debug_parser (stderr, &ref);
600}
601
602DEBUG_FUNCTION void
603debug (cp_parser *ptr)
604{
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
609}
610
611/* Allocate memory for a new lexer object and return it. */
612
613static cp_lexer *
614cp_lexer_alloc (void)
615{
616 cp_lexer *lexer;
617
618 c_common_no_more_pch ();
619
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
622
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
625
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630
631 return lexer;
632}
633
634
635/* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
637
638static cp_lexer *
639cp_lexer_new_main (void)
640{
641 cp_lexer *lexer;
642 cp_token token;
643
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
648
649 lexer = cp_lexer_alloc ();
650
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
653
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
656 {
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
659 }
660
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
667
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
671
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
674}
675
676/* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
678
679static cp_lexer *
680cp_lexer_new_from_tokens (cp_token_cache *cache)
681{
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
685
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
690
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
692
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
695
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
698}
699
700/* Frees all resources associated with LEXER. */
701
702static void
703cp_lexer_destroy (cp_lexer *lexer)
704{
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
708}
709
710/* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
714
715#define LEXER_DEBUGGING_ENABLED_P false
716
717/* Returns nonzero if debugging information should be output. */
718
719static inline bool
720cp_lexer_debugging_p (cp_lexer *lexer)
721{
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
724
725 return lexer->debugging_p;
726}
727
728
729static inline cp_token_position
730cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
731{
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
733
734 return lexer->next_token - previous_p;
735}
736
737static inline cp_token *
738cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
739{
740 return pos;
741}
742
743static inline void
744cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
745{
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
747}
748
749static inline cp_token_position
750cp_lexer_previous_token_position (cp_lexer *lexer)
751{
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
756}
757
758static inline cp_token *
759cp_lexer_previous_token (cp_lexer *lexer)
760{
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
762
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
765 {
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
768 }
769
770 return cp_lexer_token_at (lexer, tp);
771}
772
773/* nonzero if we are presently saving tokens. */
774
775static inline int
776cp_lexer_saving_tokens (const cp_lexer* lexer)
777{
778 return lexer->saved_tokens.length () != 0;
779}
780
781/* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
785
786static void
787cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
788{
789 static int is_extern_c = 0;
790
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
798
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
805
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
808 {
809 if (C_IS_RESERVED_WORD (token->u.value))
810 {
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
815 }
816 else
817 {
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
821 {
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
827
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
831 }
832
833 token->keyword = RID_MAX;
834 }
835 }
836 else if (token->type == CPP_AT_NAME)
837 {
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
841 {
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
849 */
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
859 }
860 }
861}
862
863/* Update the globals input_location and the input file stack from TOKEN. */
864static inline void
865cp_lexer_set_source_position_from_token (cp_token *token)
866{
867 if (token->type != CPP_EOF)
868 {
869 input_location = token->location;
870 }
871}
872
873/* Update the globals input_location and the input file stack from LEXER. */
874static inline void
875cp_lexer_set_source_position (cp_lexer *lexer)
876{
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
879}
880
881/* Return a pointer to the next token in the token stream, but do not
882 consume it. */
883
884static inline cp_token *
885cp_lexer_peek_token (cp_lexer *lexer)
886{
887 if (cp_lexer_debugging_p (lexer))
888 {
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
892 }
893 return lexer->next_token;
894}
895
896/* Return true if the next token has the indicated TYPE. */
897
898static inline bool
899cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
900{
901 return cp_lexer_peek_token (lexer)->type == type;
902}
903
904/* Return true if the next token does not have the indicated TYPE. */
905
906static inline bool
907cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
908{
909 return !cp_lexer_next_token_is (lexer, type);
910}
911
912/* Return true if the next token is the indicated KEYWORD. */
913
914static inline bool
915cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
916{
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
918}
919
920static inline bool
921cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
922{
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
924}
925
926static inline bool
927cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
928{
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
930}
931
932/* Return true if the next token is not the indicated KEYWORD. */
933
934static inline bool
935cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
936{
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
938}
939
940/* Return true if KEYWORD can start a decl-specifier. */
941
942bool
943cp_keyword_starts_decl_specifier_p (enum rid keyword)
944{
945 switch (keyword)
946 {
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
984
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
991 }
992}
993
994/* Return true if the next token is a keyword for a decl-specifier. */
995
996static bool
997cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
998{
999 cp_token *token;
1000
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1003}
1004
1005/* Returns TRUE iff the token T begins a decltype type. */
1006
1007static bool
1008token_is_decltype (cp_token *t)
1009{
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1012}
1013
1014/* Returns TRUE iff the next token begins a decltype type. */
1015
1016static bool
1017cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1018{
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1021}
1022
1023/* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1025
1026static tree
1027saved_checks_value (struct tree_check *check_value)
1028{
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1034 {
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1040 }
1041 /* Return the stored value. */
1042 return check_value->value;
1043}
1044
1045/* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1050
1051static cp_token *
1052cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1053{
1054 cp_token *token;
1055
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1058
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1062
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1067 {
1068 ++token;
1069 if (token == lexer->last_token)
1070 {
1071 token = &eof_token;
1072 break;
1073 }
1074
1075 if (!token->purged_p)
1076 --n;
1077 }
1078
1079 if (cp_lexer_debugging_p (lexer))
1080 {
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1083 }
1084
1085 return token;
1086}
1087
1088/* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1090
1091static cp_token *
1092cp_lexer_consume_token (cp_lexer* lexer)
1093{
1094 cp_token *token = lexer->next_token;
1095
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1098
1099 do
1100 {
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1103 {
1104 lexer->next_token = &eof_token;
1105 break;
1106 }
1107
1108 }
1109 while (lexer->next_token->purged_p);
1110
1111 cp_lexer_set_source_position_from_token (token);
1112
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1115 {
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1119 }
1120
1121 return token;
1122}
1123
1124/* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1127
1128static void
1129cp_lexer_purge_token (cp_lexer *lexer)
1130{
1131 cp_token *tok = lexer->next_token;
1132
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1138
1139 do
1140 {
1141 tok++;
1142 if (tok == lexer->last_token)
1143 {
1144 tok = &eof_token;
1145 break;
1146 }
1147 }
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1150}
1151
1152/* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1155
1156static void
1157cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1158{
1159 cp_token *peek = lexer->next_token;
1160
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1163
1164 gcc_assert (tok < peek);
1165
1166 for ( tok += 1; tok != peek; tok += 1)
1167 {
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1172 }
1173}
1174
1175/* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1177
1178static void
1179cp_lexer_save_tokens (cp_lexer* lexer)
1180{
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1184
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1186}
1187
1188/* Commit to the portion of the token stream most recently saved. */
1189
1190static void
1191cp_lexer_commit_tokens (cp_lexer* lexer)
1192{
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1196
1197 lexer->saved_tokens.pop ();
1198}
1199
1200/* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1202
1203static void
1204cp_lexer_rollback_tokens (cp_lexer* lexer)
1205{
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1209
1210 lexer->next_token = lexer->saved_tokens.pop ();
1211}
1212
1213/* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1217
1218struct saved_token_sentinel
1219{
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1224 {
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1227 }
1228 void rollback ()
1229 {
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1232 }
1233 ~saved_token_sentinel()
1234 {
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1238 }
1239};
1240
1241/* Print a representation of the TOKEN on the STREAM. */
1242
1243static void
1244cp_lexer_print_token (FILE * stream, cp_token *token)
1245{
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250#define OP(e, s) #e,
1251#define TK(e, s) #e,
1252 TTYPE_TABLE
1253#undef OP
1254#undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1259 };
1260
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1263 {
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1273
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1281
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value, 0);
1284 break;
1285
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1294 }
1295}
1296
1297DEBUG_FUNCTION void
1298debug (cp_token &ref)
1299{
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1302}
1303
1304DEBUG_FUNCTION void
1305debug (cp_token *ptr)
1306{
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1311}
1312
1313
1314/* Start emitting debugging information. */
1315
1316static void
1317cp_lexer_start_debugging (cp_lexer* lexer)
1318{
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1322
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1325}
1326
1327/* Stop emitting debugging information. */
1328
1329static void
1330cp_lexer_stop_debugging (cp_lexer* lexer)
1331{
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1335
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1338}
1339
1340/* Create a new cp_token_cache, representing a range of tokens. */
1341
1342static cp_token_cache *
1343cp_token_cache_new (cp_token *first, cp_token *last)
1344{
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1349}
1350
1351/* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1353
1354static inline void
1355cp_ensure_no_omp_declare_simd (cp_parser *parser)
1356{
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1358 {
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1362 }
1363}
1364
1365/* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1367
1368static inline void
1369cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1370{
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1372 {
1373 if (fndecl == error_mark_node)
1374 {
1375 parser->omp_declare_simd = NULL;
1376 return;
1377 }
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1379 {
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1382 }
1383 }
1384}
1385
1386/* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1388
1389static inline void
1390cp_ensure_no_oacc_routine (cp_parser *parser)
1391{
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1393 {
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1398 }
1399}
1400
1401/* Decl-specifiers. */
1402
1403/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1404
1405static void
1406clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1407{
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1409}
1410
1411/* Declarators. */
1412
1413/* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1417
1418static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1428
1429/* An erroneous declarator. */
1430static cp_declarator *cp_error_declarator;
1431
1432/* The obstack on which declarators and related data structures are
1433 allocated. */
1434static struct obstack declarator_obstack;
1435
1436/* Alloc BYTES from the declarator memory pool. */
1437
1438static inline void *
1439alloc_declarator (size_t bytes)
1440{
1441 return obstack_alloc (&declarator_obstack, bytes);
1442}
1443
1444/* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1446
1447static cp_declarator *
1448make_declarator (cp_declarator_kind kind)
1449{
1450 cp_declarator *declarator;
1451
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1459
1460 return declarator;
1461}
1462
1463/* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1468
1469static cp_declarator *
1470make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1472{
1473 cp_declarator *declarator;
1474
1475 /* It is valid to write:
1476
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1480
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1487
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1491
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1496
1497 return declarator;
1498}
1499
1500/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1504
1505cp_declarator *
1506make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1508{
1509 cp_declarator *declarator;
1510
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1516 {
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1520 }
1521 else
1522 declarator->parameter_pack_p = false;
1523
1524 declarator->std_attributes = attributes;
1525
1526 return declarator;
1527}
1528
1529/* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1532
1533cp_declarator *
1534make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1536{
1537 cp_declarator *declarator;
1538
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1544 {
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1548 }
1549 else
1550 declarator->parameter_pack_p = false;
1551
1552 declarator->std_attributes = attributes;
1553
1554 return declarator;
1555}
1556
1557/* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1560
1561cp_declarator *
1562make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1565{
1566 cp_declarator *declarator;
1567
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1572
1573 if (pointee)
1574 {
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1577 }
1578 else
1579 declarator->parameter_pack_p = false;
1580
1581 declarator->std_attributes = attributes;
1582
1583 return declarator;
1584}
1585
1586/* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1590
1591cp_declarator *
1592make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1601{
1602 cp_declarator *declarator;
1603
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1615 {
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1619 }
1620 else
1621 declarator->parameter_pack_p = false;
1622
1623 return declarator;
1624}
1625
1626/* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1628
1629cp_declarator *
1630make_array_declarator (cp_declarator *element, tree bounds)
1631{
1632 cp_declarator *declarator;
1633
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1638 {
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1642 }
1643 else
1644 declarator->parameter_pack_p = false;
1645
1646 return declarator;
1647}
1648
1649/* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651static bool
1652declarator_can_be_parameter_pack (cp_declarator *declarator)
1653{
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1657
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1664 {
1665 switch ((int)declarator->kind)
1666 {
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1672
1673 case cdk_error:
1674 return true;
1675
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1679 }
1680 }
1681
1682 return !found;
1683}
1684
1685cp_parameter_declarator *no_parameters;
1686
1687/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1689
1690cp_parameter_declarator *
1691make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1695{
1696 cp_parameter_declarator *parameter;
1697
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1708
1709 return parameter;
1710}
1711
1712/* Returns true iff DECLARATOR is a declaration for a function. */
1713
1714static bool
1715function_declarator_p (const cp_declarator *declarator)
1716{
1717 while (declarator)
1718 {
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1727 }
1728 return false;
1729}
1730
1731/* The parser. */
1732
1733/* Overview
1734 --------
1735
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1743
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1747
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1756
1757 Methodology
1758 -----------
1759
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1776
1777 Future Improvements
1778 -------------------
1779
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1785
1786/* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1788
1789enum
1790{
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1807};
1808
1809/* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811typedef int cp_parser_flags;
1812
1813/* The different kinds of declarators we want to parse. */
1814
1815enum cp_parser_declarator_kind
1816{
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1823};
1824
1825/* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1828
1829enum cp_parser_prec
1830{
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1844};
1845
1846/* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1848
1849struct cp_parser_binary_operations_map_node
1850{
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1857};
1858
1859struct cp_parser_expression_stack_entry
1860{
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1873};
1874
1875/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1880
1881/* Prototypes. */
1882
1883/* Constructors and destructors. */
1884
1885static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1887
1888/* Class variables. */
1889
1890static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1891
1892/* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1895
1896static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1899
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1903
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1906
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1909
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1914
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1917
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1919
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1921
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1923
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1925
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1927};
1928
1929/* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1933
1934/* Constructors and destructors. */
1935
1936/* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1938
1939static cp_parser_context *
1940cp_parser_context_new (cp_parser_context* next)
1941{
1942 cp_parser_context *context;
1943
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1946 {
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1951 }
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1954
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1960 {
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1966 }
1967
1968 return context;
1969}
1970
1971/* Managing the unparsed function queues. */
1972
1973#define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975#define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977#define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979#define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1981
1982static void
1983push_unparsed_function_queues (cp_parser *parser)
1984{
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1987}
1988
1989static void
1990pop_unparsed_function_queues (cp_parser *parser)
1991{
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1994}
1995
1996/* Prototypes. */
1997
1998/* Constructors and destructors. */
1999
2000static cp_parser *cp_parser_new
2001 (void);
2002
2003/* Routines to parse various constructs.
2004
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2014
2015/* Lexical conventions [gram.lex] */
2016
2017static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023static tree cp_parser_userdef_string_literal
2024 (tree);
2025static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2027
2028/* Basic concepts [gram.basic] */
2029
2030static bool cp_parser_translation_unit
2031 (cp_parser *);
2032
2033/* Expressions [gram.expr] */
2034
2035static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool, bool = false);
2043static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055/* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063static tree cp_parser_new_expression
2064 (cp_parser *);
2065static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075static tree cp_parser_delete_expression
2076 (cp_parser *);
2077static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2101
2102/* Statements [gram.stmt.stmt] */
2103
2104static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106static void cp_parser_label_for_labeled_statement
2107(cp_parser *, tree);
2108static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116static tree cp_parser_condition
2117 (cp_parser *);
2118static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122static tree cp_parser_for
2123 (cp_parser *, bool);
2124static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128static void do_range_for_auto_deduction
2129 (tree, tree);
2130static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134static tree cp_parser_jump_statement
2135 (cp_parser *);
2136static void cp_parser_declaration_statement
2137 (cp_parser *);
2138
2139static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2143
2144/* Declarations [gram.dcl.dcl] */
2145
2146static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148static void cp_parser_declaration
2149 (cp_parser *);
2150static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167static tree cp_parser_type_name
2168 (cp_parser *);
2169static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179static tree cp_parser_namespace_name
2180 (cp_parser *);
2181static void cp_parser_namespace_definition
2182 (cp_parser *);
2183static void cp_parser_namespace_body
2184 (cp_parser *);
2185static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191static void cp_parser_using_directive
2192 (cp_parser *);
2193static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195static void cp_parser_asm_definition
2196 (cp_parser *);
2197static void cp_parser_linkage_specification
2198 (cp_parser *);
2199static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201static tree cp_parser_decltype
2202 (cp_parser *);
2203static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2205
2206/* Declarators [gram.dcl.decl] */
2207
2208static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229static tree cp_parser_type_id
2230 (cp_parser *);
2231static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233static tree cp_parser_trailing_type_id (cp_parser *);
2234static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246static void cp_parser_function_body
2247 (cp_parser *, bool);
2248static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2256
2257static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2259
2260static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2262
2263static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2265
2266static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2268
2269static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2273
2274/* Classes [gram.class] */
2275
2276static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278static tree cp_parser_class_specifier
2279 (cp_parser *);
2280static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288static void cp_parser_member_declaration
2289 (cp_parser *);
2290static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292static tree cp_parser_constant_initializer
2293 (cp_parser *);
2294
2295/* Derived classes [gram.class.derived] */
2296
2297static tree cp_parser_base_clause
2298 (cp_parser *);
2299static tree cp_parser_base_specifier
2300 (cp_parser *);
2301
2302/* Special member functions [gram.special] */
2303
2304static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2318
2319/* Overloading [gram.over] */
2320
2321static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323static cp_expr cp_parser_operator
2324 (cp_parser *);
2325
2326/* Templates [gram.temp] */
2327
2328static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342static tree cp_parser_template_argument
2343 (cp_parser *);
2344static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346static void cp_parser_explicit_specialization
2347 (cp_parser *);
2348
2349/* Exception handling [gram.exception] */
2350
2351static tree cp_parser_try_block
2352 (cp_parser *);
2353static bool cp_parser_function_try_block
2354 (cp_parser *);
2355static void cp_parser_handler_seq
2356 (cp_parser *);
2357static void cp_parser_handler
2358 (cp_parser *);
2359static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361static tree cp_parser_throw_expression
2362 (cp_parser *);
2363static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365static tree cp_parser_type_id_list
2366 (cp_parser *);
2367
2368/* GNU Extensions */
2369
2370static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404static void cp_parser_label_declaration
2405 (cp_parser *);
2406
2407/* Concept Extensions */
2408
2409static tree cp_parser_requires_clause
2410 (cp_parser *);
2411static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413static tree cp_parser_requires_expression
2414 (cp_parser *);
2415static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417static tree cp_parser_requirement_body
2418 (cp_parser *);
2419static tree cp_parser_requirement_list
2420 (cp_parser *);
2421static tree cp_parser_requirement
2422 (cp_parser *);
2423static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427static tree cp_parser_type_requirement
2428 (cp_parser *);
2429static tree cp_parser_nested_requirement
2430 (cp_parser *);
2431
2432/* Transactional Memory Extensions */
2433
2434static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2442
2443enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2449};
2450static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2452
2453/* Objective-C++ Productions */
2454
2455static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473static tree cp_parser_objc_selector
2474 (cp_parser *);
2475static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479static tree cp_parser_objc_statement
2480 (cp_parser *);
2481static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2491
2492/* Utility Routines */
2493
2494static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597static void cp_parser_error
2598 (cp_parser *, const char *);
2599static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601static bool cp_parser_simulate_error
2602 (cp_parser *);
2603static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629static bool cp_parser_error_occurred
2630 (cp_parser *);
2631static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635static bool cp_parser_is_string_literal
2636 (cp_token *);
2637static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2651
2652/* Concept-related syntactic transformations */
2653
2654static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2656
2657// -------------------------------------------------------------------------- //
2658// Unevaluated Operand Guard
2659//
2660// Implementation of an RAII helper for unevaluated operand parsing.
2661cp_unevaluated::cp_unevaluated ()
2662{
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2665}
2666
2667cp_unevaluated::~cp_unevaluated ()
2668{
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2671}
2672
2673// -------------------------------------------------------------------------- //
2674// Tentative Parsing
2675
2676/* Returns nonzero if we are parsing tentatively. */
2677
2678static inline bool
2679cp_parser_parsing_tentatively (cp_parser* parser)
2680{
2681 return parser->context->next != NULL;
2682}
2683
2684/* Returns nonzero if TOKEN is a string literal. */
2685
2686static bool
2687cp_parser_is_pure_string_literal (cp_token* token)
2688{
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2694}
2695
2696/* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2698
2699static bool
2700cp_parser_is_string_literal (cp_token* token)
2701{
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2708}
2709
2710/* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711
2712static bool
2713cp_parser_is_keyword (cp_token* token, enum rid keyword)
2714{
2715 return token->keyword == keyword;
2716}
2717
2718/* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2720
2721static enum pragma_kind
2722cp_parser_pragma_kind (cp_token *token)
2723{
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728}
2729
2730/* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2740
2741static bool
2742cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2744{
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2754
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2759
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2766
2767 return true;
2768}
2769
2770/* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2775
2776static void
2777cp_parser_error (cp_parser* parser, const char* gmsgid)
2778{
2779 if (!cp_parser_simulate_error (parser))
2780 {
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2785
2786 if (token->type == CPP_PRAGMA)
2787 {
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2792 }
2793
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2798 {
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2801 {
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2804 }
2805 }
2806
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2813 }
2814}
2815
2816/* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2820
2821static void
2822cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2827{
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2831 {
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2846 }
2847 else if (parser->scope && parser->scope != global_namespace)
2848 {
2849 switch (desired)
2850 {
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2866
2867 }
2868 }
2869 else if (parser->scope == global_namespace)
2870 {
2871 switch (desired)
2872 {
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2886 }
2887 }
2888 else
2889 {
2890 switch (desired)
2891 {
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2904 }
2905 }
2906}
2907
2908/* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2911
2912static bool
2913cp_parser_simulate_error (cp_parser* parser)
2914{
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2916 {
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2919 }
2920 return false;
2921}
2922
2923/* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2926
2927static bool
2928cp_parser_check_type_definition (cp_parser* parser)
2929{
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2932 {
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2937 }
2938 return true;
2939}
2940
2941/* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2946
2947static void
2948cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2950{
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2961 {
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2967 }
2968}
2969
2970/* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2975
2976static void
2977cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2981{
2982 cp_token_position start = 0;
2983
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2985 {
2986 if (TREE_CODE (type) == TYPE_DECL)
2987 type = TREE_TYPE (type);
2988 if (TYPE_P (type) && !template_placeholder_p (type))
2989 error_at (location, "%qT is not a template", type);
2990 else if (identifier_p (type))
2991 {
2992 if (tag_type != none_type)
2993 error_at (location, "%qE is not a class template", type);
2994 else
2995 error_at (location, "%qE is not a template", type);
2996 }
2997 else
2998 error_at (location, "invalid template-id");
2999 /* Remember the location of the invalid "<". */
3000 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3001 start = cp_lexer_token_position (parser->lexer, true);
3002 /* Consume the "<". */
3003 cp_lexer_consume_token (parser->lexer);
3004 /* Parse the template arguments. */
3005 cp_parser_enclosed_template_argument_list (parser);
3006 /* Permanently remove the invalid template arguments so that
3007 this error message is not issued again. */
3008 if (start)
3009 cp_lexer_purge_tokens_after (parser->lexer, start);
3010 }
3011}
3012
3013/* If parsing an integral constant-expression, issue an error message
3014 about the fact that THING appeared and return true. Otherwise,
3015 return false. In either case, set
3016 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3017
3018static bool
3019cp_parser_non_integral_constant_expression (cp_parser *parser,
3020 non_integral_constant thing)
3021{
3022 parser->non_integral_constant_expression_p = true;
3023 if (parser->integral_constant_expression_p)
3024 {
3025 if (!parser->allow_non_integral_constant_expression_p)
3026 {
3027 const char *msg = NULL;
3028 switch (thing)
3029 {
3030 case NIC_FLOAT:
3031 pedwarn (input_location, OPT_Wpedantic,
3032 "ISO C++ forbids using a floating-point literal "
3033 "in a constant-expression");
3034 return true;
3035 case NIC_CAST:
3036 error ("a cast to a type other than an integral or "
3037 "enumeration type cannot appear in a "
3038 "constant-expression");
3039 return true;
3040 case NIC_TYPEID:
3041 error ("%<typeid%> operator "
3042 "cannot appear in a constant-expression");
3043 return true;
3044 case NIC_NCC:
3045 error ("non-constant compound literals "
3046 "cannot appear in a constant-expression");
3047 return true;
3048 case NIC_FUNC_CALL:
3049 error ("a function call "
3050 "cannot appear in a constant-expression");
3051 return true;
3052 case NIC_INC:
3053 error ("an increment "
3054 "cannot appear in a constant-expression");
3055 return true;
3056 case NIC_DEC:
3057 error ("an decrement "
3058 "cannot appear in a constant-expression");
3059 return true;
3060 case NIC_ARRAY_REF:
3061 error ("an array reference "
3062 "cannot appear in a constant-expression");
3063 return true;
3064 case NIC_ADDR_LABEL:
3065 error ("the address of a label "
3066 "cannot appear in a constant-expression");
3067 return true;
3068 case NIC_OVERLOADED:
3069 error ("calls to overloaded operators "
3070 "cannot appear in a constant-expression");
3071 return true;
3072 case NIC_ASSIGNMENT:
3073 error ("an assignment cannot appear in a constant-expression");
3074 return true;
3075 case NIC_COMMA:
3076 error ("a comma operator "
3077 "cannot appear in a constant-expression");
3078 return true;
3079 case NIC_CONSTRUCTOR:
3080 error ("a call to a constructor "
3081 "cannot appear in a constant-expression");
3082 return true;
3083 case NIC_TRANSACTION:
3084 error ("a transaction expression "
3085 "cannot appear in a constant-expression");
3086 return true;
3087 case NIC_THIS:
3088 msg = "this";
3089 break;
3090 case NIC_FUNC_NAME:
3091 msg = "__FUNCTION__";
3092 break;
3093 case NIC_PRETTY_FUNC:
3094 msg = "__PRETTY_FUNCTION__";
3095 break;
3096 case NIC_C99_FUNC:
3097 msg = "__func__";
3098 break;
3099 case NIC_VA_ARG:
3100 msg = "va_arg";
3101 break;
3102 case NIC_ARROW:
3103 msg = "->";
3104 break;
3105 case NIC_POINT:
3106 msg = ".";
3107 break;
3108 case NIC_STAR:
3109 msg = "*";
3110 break;
3111 case NIC_ADDR:
3112 msg = "&";
3113 break;
3114 case NIC_PREINCREMENT:
3115 msg = "++";
3116 break;
3117 case NIC_PREDECREMENT:
3118 msg = "--";
3119 break;
3120 case NIC_NEW:
3121 msg = "new";
3122 break;
3123 case NIC_DEL:
3124 msg = "delete";
3125 break;
3126 default:
3127 gcc_unreachable ();
3128 }
3129 if (msg)
3130 error ("%qs cannot appear in a constant-expression", msg);
3131 return true;
3132 }
3133 }
3134 return false;
3135}
3136
3137/* Emit a diagnostic for an invalid type name. This function commits
3138 to the current active tentative parse, if any. (Otherwise, the
3139 problematic construct might be encountered again later, resulting
3140 in duplicate error messages.) LOCATION is the location of ID. */
3141
3142static void
3143cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3144 location_t location)
3145{
3146 tree decl, ambiguous_decls;
3147 cp_parser_commit_to_tentative_parse (parser);
3148 /* Try to lookup the identifier. */
3149 decl = cp_parser_lookup_name (parser, id, none_type,
3150 /*is_template=*/false,
3151 /*is_namespace=*/false,
3152 /*check_dependency=*/true,
3153 &ambiguous_decls, location);
3154 if (ambiguous_decls)
3155 /* If the lookup was ambiguous, an error will already have
3156 been issued. */
3157 return;
3158 /* If the lookup found a template-name, it means that the user forgot
3159 to specify an argument list. Emit a useful error message. */
3160 if (DECL_TYPE_TEMPLATE_P (decl))
3161 {
3162 error_at (location,
3163 "invalid use of template-name %qE without an argument list",
3164 decl);
3165 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3166 inform (location, "class template argument deduction is only available "
3167 "with -std=c++1z or -std=gnu++1z");
3168 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3169 }
3170 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3171 error_at (location, "invalid use of destructor %qD as a type", id);
3172 else if (TREE_CODE (decl) == TYPE_DECL)
3173 /* Something like 'unsigned A a;' */
3174 error_at (location, "invalid combination of multiple type-specifiers");
3175 else if (!parser->scope)
3176 {
3177 /* Issue an error message. */
3178 const char *suggestion = NULL;
3179 if (TREE_CODE (id) == IDENTIFIER_NODE)
3180 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3181 if (suggestion)
3182 {
3183 gcc_rich_location richloc (location);
3184 richloc.add_fixit_replace (suggestion);
3185 error_at_rich_loc (&richloc,
3186 "%qE does not name a type; did you mean %qs?",
3187 id, suggestion);
3188 }
3189 else
3190 error_at (location, "%qE does not name a type", id);
3191 /* If we're in a template class, it's possible that the user was
3192 referring to a type from a base class. For example:
3193
3194 template <typename T> struct A { typedef T X; };
3195 template <typename T> struct B : public A<T> { X x; };
3196
3197 The user should have said "typename A<T>::X". */
3198 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3199 inform (location, "C++11 %<constexpr%> only available with "
3200 "-std=c++11 or -std=gnu++11");
3201 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3202 inform (location, "C++11 %<noexcept%> only available with "
3203 "-std=c++11 or -std=gnu++11");
3204 else if (cxx_dialect < cxx11
3205 && TREE_CODE (id) == IDENTIFIER_NODE
3206 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3207 inform (location, "C++11 %<thread_local%> only available with "
3208 "-std=c++11 or -std=gnu++11");
3209 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3210 inform (location, "%<concept%> only available with -fconcepts");
3211 else if (processing_template_decl && current_class_type
3212 && TYPE_BINFO (current_class_type))
3213 {
3214 tree b;
3215
3216 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3217 b;
3218 b = TREE_CHAIN (b))
3219 {
3220 tree base_type = BINFO_TYPE (b);
3221 if (CLASS_TYPE_P (base_type)
3222 && dependent_type_p (base_type))
3223 {
3224 tree field;
3225 /* Go from a particular instantiation of the
3226 template (which will have an empty TYPE_FIELDs),
3227 to the main version. */
3228 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3229 for (field = TYPE_FIELDS (base_type);
3230 field;
3231 field = DECL_CHAIN (field))
3232 if (TREE_CODE (field) == TYPE_DECL
3233 && DECL_NAME (field) == id)
3234 {
3235 inform (location,
3236 "(perhaps %<typename %T::%E%> was intended)",
3237 BINFO_TYPE (b), id);
3238 break;
3239 }
3240 if (field)
3241 break;
3242 }
3243 }
3244 }
3245 }
3246 /* Here we diagnose qualified-ids where the scope is actually correct,
3247 but the identifier does not resolve to a valid type name. */
3248 else if (parser->scope != error_mark_node)
3249 {
3250 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3251 {
3252 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3253 error_at (location_of (id),
3254 "%qE in namespace %qE does not name a template type",
3255 id, parser->scope);
3256 else
3257 error_at (location_of (id),
3258 "%qE in namespace %qE does not name a type",
3259 id, parser->scope);
3260 if (DECL_P (decl))
3261 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3262 }
3263 else if (CLASS_TYPE_P (parser->scope)
3264 && constructor_name_p (id, parser->scope))
3265 {
3266 /* A<T>::A<T>() */
3267 error_at (location, "%<%T::%E%> names the constructor, not"
3268 " the type", parser->scope, id);
3269 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3270 error_at (location, "and %qT has no template constructors",
3271 parser->scope);
3272 }
3273 else if (TYPE_P (parser->scope)
3274 && dependent_scope_p (parser->scope))
3275 error_at (location, "need %<typename%> before %<%T::%E%> because "
3276 "%qT is a dependent scope",
3277 parser->scope, id, parser->scope);
3278 else if (TYPE_P (parser->scope))
3279 {
3280 if (!COMPLETE_TYPE_P (parser->scope))
3281 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3282 parser->scope);
3283 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3284 error_at (location_of (id),
3285 "%qE in %q#T does not name a template type",
3286 id, parser->scope);
3287 else
3288 error_at (location_of (id),
3289 "%qE in %q#T does not name a type",
3290 id, parser->scope);
3291 if (DECL_P (decl))
3292 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3293 }
3294 else
3295 gcc_unreachable ();
3296 }
3297}
3298
3299/* Check for a common situation where a type-name should be present,
3300 but is not, and issue a sensible error message. Returns true if an
3301 invalid type-name was detected.
3302
3303 The situation handled by this function are variable declarations of the
3304 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3305 Usually, `ID' should name a type, but if we got here it means that it
3306 does not. We try to emit the best possible error message depending on
3307 how exactly the id-expression looks like. */
3308
3309static bool
3310cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3311{
3312 tree id;
3313 cp_token *token = cp_lexer_peek_token (parser->lexer);
3314
3315 /* Avoid duplicate error about ambiguous lookup. */
3316 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3317 {
3318 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3319 if (next->type == CPP_NAME && next->error_reported)
3320 goto out;
3321 }
3322
3323 cp_parser_parse_tentatively (parser);
3324 id = cp_parser_id_expression (parser,
3325 /*template_keyword_p=*/false,
3326 /*check_dependency_p=*/true,
3327 /*template_p=*/NULL,
3328 /*declarator_p=*/true,
3329 /*optional_p=*/false);
3330 /* If the next token is a (, this is a function with no explicit return
3331 type, i.e. constructor, destructor or conversion op. */
3332 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3333 || TREE_CODE (id) == TYPE_DECL)
3334 {
3335 cp_parser_abort_tentative_parse (parser);
3336 return false;
3337 }
3338 if (!cp_parser_parse_definitely (parser))
3339 return false;
3340
3341 /* Emit a diagnostic for the invalid type. */
3342 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3343 out:
3344 /* If we aren't in the middle of a declarator (i.e. in a
3345 parameter-declaration-clause), skip to the end of the declaration;
3346 there's no point in trying to process it. */
3347 if (!parser->in_declarator_p)
3348 cp_parser_skip_to_end_of_block_or_statement (parser);
3349 return true;
3350}
3351
3352/* Consume tokens up to, and including, the next non-nested closing `)'.
3353 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3354 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3355 found an unnested token of that type. */
3356
3357static int
3358cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3359 bool recovering,
3360 cpp_ttype or_ttype,
3361 bool consume_paren)
3362{
3363 unsigned paren_depth = 0;
3364 unsigned brace_depth = 0;
3365 unsigned square_depth = 0;
3366
3367 if (recovering && or_ttype == CPP_EOF
3368 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3369 return 0;
3370
3371 while (true)
3372 {
3373 cp_token * token = cp_lexer_peek_token (parser->lexer);
3374
3375 /* Have we found what we're looking for before the closing paren? */
3376 if (token->type == or_ttype && or_ttype != CPP_EOF
3377 && !brace_depth && !paren_depth && !square_depth)
3378 return -1;
3379
3380 switch (token->type)
3381 {
3382 case CPP_EOF:
3383 case CPP_PRAGMA_EOL:
3384 /* If we've run out of tokens, then there is no closing `)'. */
3385 return 0;
3386
3387 /* This is good for lambda expression capture-lists. */
3388 case CPP_OPEN_SQUARE:
3389 ++square_depth;
3390 break;
3391 case CPP_CLOSE_SQUARE:
3392 if (!square_depth--)
3393 return 0;
3394 break;
3395
3396 case CPP_SEMICOLON:
3397 /* This matches the processing in skip_to_end_of_statement. */
3398 if (!brace_depth)
3399 return 0;
3400 break;
3401
3402 case CPP_OPEN_BRACE:
3403 ++brace_depth;
3404 break;
3405 case CPP_CLOSE_BRACE:
3406 if (!brace_depth--)
3407 return 0;
3408 break;
3409
3410 case CPP_OPEN_PAREN:
3411 if (!brace_depth)
3412 ++paren_depth;
3413 break;
3414
3415 case CPP_CLOSE_PAREN:
3416 if (!brace_depth && !paren_depth--)
3417 {
3418 if (consume_paren)
3419 cp_lexer_consume_token (parser->lexer);
3420 return 1;
3421 }
3422 break;
3423
3424 default:
3425 break;
3426 }
3427
3428 /* Consume the token. */
3429 cp_lexer_consume_token (parser->lexer);
3430 }
3431}
3432
3433/* Consume tokens up to, and including, the next non-nested closing `)'.
3434 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3435 are doing error recovery. Returns -1 if OR_COMMA is true and we
3436 found an unnested token of that type. */
3437
3438static int
3439cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3440 bool recovering,
3441 bool or_comma,
3442 bool consume_paren)
3443{
3444 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3445 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3446 ttype, consume_paren);
3447}
3448
3449/* Consume tokens until we reach the end of the current statement.
3450 Normally, that will be just before consuming a `;'. However, if a
3451 non-nested `}' comes first, then we stop before consuming that. */
3452
3453static void
3454cp_parser_skip_to_end_of_statement (cp_parser* parser)
3455{
3456 unsigned nesting_depth = 0;
3457
3458 /* Unwind generic function template scope if necessary. */
3459 if (parser->fully_implicit_function_template_p)
3460 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3461
3462 while (true)
3463 {
3464 cp_token *token = cp_lexer_peek_token (parser->lexer);
3465
3466 switch (token->type)
3467 {
3468 case CPP_EOF:
3469 case CPP_PRAGMA_EOL:
3470 /* If we've run out of tokens, stop. */
3471 return;
3472
3473 case CPP_SEMICOLON:
3474 /* If the next token is a `;', we have reached the end of the
3475 statement. */
3476 if (!nesting_depth)
3477 return;
3478 break;
3479
3480 case CPP_CLOSE_BRACE:
3481 /* If this is a non-nested '}', stop before consuming it.
3482 That way, when confronted with something like:
3483
3484 { 3 + }
3485
3486 we stop before consuming the closing '}', even though we
3487 have not yet reached a `;'. */
3488 if (nesting_depth == 0)
3489 return;
3490
3491 /* If it is the closing '}' for a block that we have
3492 scanned, stop -- but only after consuming the token.
3493 That way given:
3494
3495 void f g () { ... }
3496 typedef int I;
3497
3498 we will stop after the body of the erroneously declared
3499 function, but before consuming the following `typedef'
3500 declaration. */
3501 if (--nesting_depth == 0)
3502 {
3503 cp_lexer_consume_token (parser->lexer);
3504 return;
3505 }
3506 break;
3507
3508 case CPP_OPEN_BRACE:
3509 ++nesting_depth;
3510 break;
3511
3512 default:
3513 break;
3514 }
3515
3516 /* Consume the token. */
3517 cp_lexer_consume_token (parser->lexer);
3518 }
3519}
3520
3521/* This function is called at the end of a statement or declaration.
3522 If the next token is a semicolon, it is consumed; otherwise, error
3523 recovery is attempted. */
3524
3525static void
3526cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3527{
3528 /* Look for the trailing `;'. */
3529 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3530 {
3531 /* If there is additional (erroneous) input, skip to the end of
3532 the statement. */
3533 cp_parser_skip_to_end_of_statement (parser);
3534 /* If the next token is now a `;', consume it. */
3535 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3536 cp_lexer_consume_token (parser->lexer);
3537 }
3538}
3539
3540/* Skip tokens until we have consumed an entire block, or until we
3541 have consumed a non-nested `;'. */
3542
3543static void
3544cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3545{
3546 int nesting_depth = 0;
3547
3548 /* Unwind generic function template scope if necessary. */
3549 if (parser->fully_implicit_function_template_p)
3550 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3551
3552 while (nesting_depth >= 0)
3553 {
3554 cp_token *token = cp_lexer_peek_token (parser->lexer);
3555
3556 switch (token->type)
3557 {
3558 case CPP_EOF:
3559 case CPP_PRAGMA_EOL:
3560 /* If we've run out of tokens, stop. */
3561 return;
3562
3563 case CPP_SEMICOLON:
3564 /* Stop if this is an unnested ';'. */
3565 if (!nesting_depth)
3566 nesting_depth = -1;
3567 break;
3568
3569 case CPP_CLOSE_BRACE:
3570 /* Stop if this is an unnested '}', or closes the outermost
3571 nesting level. */
3572 nesting_depth--;
3573 if (nesting_depth < 0)
3574 return;
3575 if (!nesting_depth)
3576 nesting_depth = -1;
3577 break;
3578
3579 case CPP_OPEN_BRACE:
3580 /* Nest. */
3581 nesting_depth++;
3582 break;
3583
3584 default:
3585 break;
3586 }
3587
3588 /* Consume the token. */
3589 cp_lexer_consume_token (parser->lexer);
3590 }
3591}
3592
3593/* Skip tokens until a non-nested closing curly brace is the next
3594 token, or there are no more tokens. Return true in the first case,
3595 false otherwise. */
3596
3597static bool
3598cp_parser_skip_to_closing_brace (cp_parser *parser)
3599{
3600 unsigned nesting_depth = 0;
3601
3602 while (true)
3603 {
3604 cp_token *token = cp_lexer_peek_token (parser->lexer);
3605
3606 switch (token->type)
3607 {
3608 case CPP_EOF:
3609 case CPP_PRAGMA_EOL:
3610 /* If we've run out of tokens, stop. */
3611 return false;
3612
3613 case CPP_CLOSE_BRACE:
3614 /* If the next token is a non-nested `}', then we have reached
3615 the end of the current block. */
3616 if (nesting_depth-- == 0)
3617 return true;
3618 break;
3619
3620 case CPP_OPEN_BRACE:
3621 /* If it the next token is a `{', then we are entering a new
3622 block. Consume the entire block. */
3623 ++nesting_depth;
3624 break;
3625
3626 default:
3627 break;
3628 }
3629
3630 /* Consume the token. */
3631 cp_lexer_consume_token (parser->lexer);
3632 }
3633}
3634
3635/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3636 parameter is the PRAGMA token, allowing us to purge the entire pragma
3637 sequence. */
3638
3639static void
3640cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3641{
3642 cp_token *token;
3643
3644 parser->lexer->in_pragma = false;
3645
3646 do
3647 token = cp_lexer_consume_token (parser->lexer);
3648 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3649
3650 /* Ensure that the pragma is not parsed again. */
3651 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3652}
3653
3654/* Require pragma end of line, resyncing with it as necessary. The
3655 arguments are as for cp_parser_skip_to_pragma_eol. */
3656
3657static void
3658cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3659{
3660 parser->lexer->in_pragma = false;
3661 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3662 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3663}
3664
3665/* This is a simple wrapper around make_typename_type. When the id is
3666 an unresolved identifier node, we can provide a superior diagnostic
3667 using cp_parser_diagnose_invalid_type_name. */
3668
3669static tree
3670cp_parser_make_typename_type (cp_parser *parser, tree id,
3671 location_t id_location)
3672{
3673 tree result;
3674 if (identifier_p (id))
3675 {
3676 result = make_typename_type (parser->scope, id, typename_type,
3677 /*complain=*/tf_none);
3678 if (result == error_mark_node)
3679 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3680 return result;
3681 }
3682 return make_typename_type (parser->scope, id, typename_type, tf_error);
3683}
3684
3685/* This is a wrapper around the
3686 make_{pointer,ptrmem,reference}_declarator functions that decides
3687 which one to call based on the CODE and CLASS_TYPE arguments. The
3688 CODE argument should be one of the values returned by
3689 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3690 appertain to the pointer or reference. */
3691
3692static cp_declarator *
3693cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3694 cp_cv_quals cv_qualifiers,
3695 cp_declarator *target,
3696 tree attributes)
3697{
3698 if (code == ERROR_MARK)
3699 return cp_error_declarator;
3700
3701 if (code == INDIRECT_REF)
3702 if (class_type == NULL_TREE)
3703 return make_pointer_declarator (cv_qualifiers, target, attributes);
3704 else
3705 return make_ptrmem_declarator (cv_qualifiers, class_type,
3706 target, attributes);
3707 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3708 return make_reference_declarator (cv_qualifiers, target,
3709 false, attributes);
3710 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3711 return make_reference_declarator (cv_qualifiers, target,
3712 true, attributes);
3713 gcc_unreachable ();
3714}
3715
3716/* Create a new C++ parser. */
3717
3718static cp_parser *
3719cp_parser_new (void)
3720{
3721 cp_parser *parser;
3722 cp_lexer *lexer;
3723 unsigned i;
3724
3725 /* cp_lexer_new_main is called before doing GC allocation because
3726 cp_lexer_new_main might load a PCH file. */
3727 lexer = cp_lexer_new_main ();
3728
3729 /* Initialize the binops_by_token so that we can get the tree
3730 directly from the token. */
3731 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3732 binops_by_token[binops[i].token_type] = binops[i];
3733
3734 parser = ggc_cleared_alloc<cp_parser> ();
3735 parser->lexer = lexer;
3736 parser->context = cp_parser_context_new (NULL);
3737
3738 /* For now, we always accept GNU extensions. */
3739 parser->allow_gnu_extensions_p = 1;
3740
3741 /* The `>' token is a greater-than operator, not the end of a
3742 template-id. */
3743 parser->greater_than_is_operator_p = true;
3744
3745 parser->default_arg_ok_p = true;
3746
3747 /* We are not parsing a constant-expression. */
3748 parser->integral_constant_expression_p = false;
3749 parser->allow_non_integral_constant_expression_p = false;
3750 parser->non_integral_constant_expression_p = false;
3751
3752 /* Local variable names are not forbidden. */
3753 parser->local_variables_forbidden_p = false;
3754
3755 /* We are not processing an `extern "C"' declaration. */
3756 parser->in_unbraced_linkage_specification_p = false;
3757
3758 /* We are not processing a declarator. */
3759 parser->in_declarator_p = false;
3760
3761 /* We are not processing a template-argument-list. */
3762 parser->in_template_argument_list_p = false;
3763
3764 /* We are not in an iteration statement. */
3765 parser->in_statement = 0;
3766
3767 /* We are not in a switch statement. */
3768 parser->in_switch_statement_p = false;
3769
3770 /* We are not parsing a type-id inside an expression. */
3771 parser->in_type_id_in_expr_p = false;
3772
3773 /* Declarations aren't implicitly extern "C". */
3774 parser->implicit_extern_c = false;
3775
3776 /* String literals should be translated to the execution character set. */
3777 parser->translate_strings_p = true;
3778
3779 /* We are not parsing a function body. */
3780 parser->in_function_body = false;
3781
3782 /* We can correct until told otherwise. */
3783 parser->colon_corrects_to_scope_p = true;
3784
3785 /* The unparsed function queue is empty. */
3786 push_unparsed_function_queues (parser);
3787
3788 /* There are no classes being defined. */
3789 parser->num_classes_being_defined = 0;
3790
3791 /* No template parameters apply. */
3792 parser->num_template_parameter_lists = 0;
3793
3794 /* Special parsing data structures. */
3795 parser->omp_declare_simd = NULL;
3796 parser->cilk_simd_fn_info = NULL;
3797 parser->oacc_routine = NULL;
3798
3799 /* Not declaring an implicit function template. */
3800 parser->auto_is_implicit_function_template_parm_p = false;
3801 parser->fully_implicit_function_template_p = false;
3802 parser->implicit_template_parms = 0;
3803 parser->implicit_template_scope = 0;
3804
3805 /* Allow constrained-type-specifiers. */
3806 parser->prevent_constrained_type_specifiers = 0;
3807
3808 return parser;
3809}
3810
3811/* Create a cp_lexer structure which will emit the tokens in CACHE
3812 and push it onto the parser's lexer stack. This is used for delayed
3813 parsing of in-class method bodies and default arguments, and should
3814 not be confused with tentative parsing. */
3815static void
3816cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3817{
3818 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3819 lexer->next = parser->lexer;
3820 parser->lexer = lexer;
3821
3822 /* Move the current source position to that of the first token in the
3823 new lexer. */
3824 cp_lexer_set_source_position_from_token (lexer->next_token);
3825}
3826
3827/* Pop the top lexer off the parser stack. This is never used for the
3828 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3829static void
3830cp_parser_pop_lexer (cp_parser *parser)
3831{
3832 cp_lexer *lexer = parser->lexer;
3833 parser->lexer = lexer->next;
3834 cp_lexer_destroy (lexer);
3835
3836 /* Put the current source position back where it was before this
3837 lexer was pushed. */
3838 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3839}
3840
3841/* Lexical conventions [gram.lex] */
3842
3843/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3844 identifier. */
3845
3846static cp_expr
3847cp_parser_identifier (cp_parser* parser)
3848{
3849 cp_token *token;
3850
3851 /* Look for the identifier. */
3852 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3853 /* Return the value. */
3854 if (token)
3855 return cp_expr (token->u.value, token->location);
3856 else
3857 return error_mark_node;
3858}
3859
3860/* Parse a sequence of adjacent string constants. Returns a
3861 TREE_STRING representing the combined, nul-terminated string
3862 constant. If TRANSLATE is true, translate the string to the
3863 execution character set. If WIDE_OK is true, a wide string is
3864 invalid here.
3865
3866 C++98 [lex.string] says that if a narrow string literal token is
3867 adjacent to a wide string literal token, the behavior is undefined.
3868 However, C99 6.4.5p4 says that this results in a wide string literal.
3869 We follow C99 here, for consistency with the C front end.
3870
3871 This code is largely lifted from lex_string() in c-lex.c.
3872
3873 FUTURE: ObjC++ will need to handle @-strings here. */
3874static cp_expr
3875cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3876 bool lookup_udlit = true)
3877{
3878 tree value;
3879 size_t count;
3880 struct obstack str_ob;
3881 cpp_string str, istr, *strs;
3882 cp_token *tok;
3883 enum cpp_ttype type, curr_type;
3884 int have_suffix_p = 0;
3885 tree string_tree;
3886 tree suffix_id = NULL_TREE;
3887 bool curr_tok_is_userdef_p = false;
3888
3889 tok = cp_lexer_peek_token (parser->lexer);
3890 if (!cp_parser_is_string_literal (tok))
3891 {
3892 cp_parser_error (parser, "expected string-literal");
3893 return error_mark_node;
3894 }
3895
3896 location_t loc = tok->location;
3897
3898 if (cpp_userdef_string_p (tok->type))
3899 {
3900 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3901 curr_type = cpp_userdef_string_remove_type (tok->type);
3902 curr_tok_is_userdef_p = true;
3903 }
3904 else
3905 {
3906 string_tree = tok->u.value;
3907 curr_type = tok->type;
3908 }
3909 type = curr_type;
3910
3911 /* Try to avoid the overhead of creating and destroying an obstack
3912 for the common case of just one string. */
3913 if (!cp_parser_is_string_literal
3914 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3915 {
3916 cp_lexer_consume_token (parser->lexer);
3917
3918 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3919 str.len = TREE_STRING_LENGTH (string_tree);
3920 count = 1;
3921
3922 if (curr_tok_is_userdef_p)
3923 {
3924 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3925 have_suffix_p = 1;
3926 curr_type = cpp_userdef_string_remove_type (tok->type);
3927 }
3928 else
3929 curr_type = tok->type;
3930
3931 strs = &str;
3932 }
3933 else
3934 {
3935 location_t last_tok_loc = tok->location;
3936 gcc_obstack_init (&str_ob);
3937 count = 0;
3938
3939 do
3940 {
3941 cp_lexer_consume_token (parser->lexer);
3942 count++;
3943 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3944 str.len = TREE_STRING_LENGTH (string_tree);
3945
3946 if (curr_tok_is_userdef_p)
3947 {
3948 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3949 if (have_suffix_p == 0)
3950 {
3951 suffix_id = curr_suffix_id;
3952 have_suffix_p = 1;
3953 }
3954 else if (have_suffix_p == 1
3955 && curr_suffix_id != suffix_id)
3956 {
3957 error ("inconsistent user-defined literal suffixes"
3958 " %qD and %qD in string literal",
3959 suffix_id, curr_suffix_id);
3960 have_suffix_p = -1;
3961 }
3962 curr_type = cpp_userdef_string_remove_type (tok->type);
3963 }
3964 else
3965 curr_type = tok->type;
3966
3967 if (type != curr_type)
3968 {
3969 if (type == CPP_STRING)
3970 type = curr_type;
3971 else if (curr_type != CPP_STRING)
3972 {
3973 rich_location rich_loc (line_table, tok->location);
3974 rich_loc.add_range (last_tok_loc, false);
3975 error_at_rich_loc (&rich_loc,
3976 "unsupported non-standard concatenation "
3977 "of string literals");
3978 }
3979 }
3980
3981 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3982
3983 last_tok_loc = tok->location;
3984
3985 tok = cp_lexer_peek_token (parser->lexer);
3986 if (cpp_userdef_string_p (tok->type))
3987 {
3988 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3989 curr_type = cpp_userdef_string_remove_type (tok->type);
3990 curr_tok_is_userdef_p = true;
3991 }
3992 else
3993 {
3994 string_tree = tok->u.value;
3995 curr_type = tok->type;
3996 curr_tok_is_userdef_p = false;
3997 }
3998 }
3999 while (cp_parser_is_string_literal (tok));
4000
4001 /* A string literal built by concatenation has its caret=start at
4002 the start of the initial string, and its finish at the finish of
4003 the final string literal. */
4004 loc = make_location (loc, loc, get_finish (last_tok_loc));
4005
4006 strs = (cpp_string *) obstack_finish (&str_ob);
4007 }
4008
4009 if (type != CPP_STRING && !wide_ok)
4010 {
4011 cp_parser_error (parser, "a wide string is invalid in this context");
4012 type = CPP_STRING;
4013 }
4014
4015 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4016 (parse_in, strs, count, &istr, type))
4017 {
4018 value = build_string (istr.len, (const char *)istr.text);
4019 free (CONST_CAST (unsigned char *, istr.text));
4020
4021 switch (type)
4022 {
4023 default:
4024 case CPP_STRING:
4025 case CPP_UTF8STRING:
4026 TREE_TYPE (value) = char_array_type_node;
4027 break;
4028 case CPP_STRING16:
4029 TREE_TYPE (value) = char16_array_type_node;
4030 break;
4031 case CPP_STRING32:
4032 TREE_TYPE (value) = char32_array_type_node;
4033 break;
4034 case CPP_WSTRING:
4035 TREE_TYPE (value) = wchar_array_type_node;
4036 break;
4037 }
4038
4039 value = fix_string_type (value);
4040
4041 if (have_suffix_p)
4042 {
4043 tree literal = build_userdef_literal (suffix_id, value,
4044 OT_NONE, NULL_TREE);
4045 if (lookup_udlit)
4046 value = cp_parser_userdef_string_literal (literal);
4047 else
4048 value = literal;
4049 }
4050 }
4051 else
4052 /* cpp_interpret_string has issued an error. */
4053 value = error_mark_node;
4054
4055 if (count > 1)
4056 obstack_free (&str_ob, 0);
4057
4058 return cp_expr (value, loc);
4059}
4060
4061/* Look up a literal operator with the name and the exact arguments. */
4062
4063static tree
4064lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4065{
4066 tree decl, fns;
4067 decl = lookup_name (name);
4068 if (!decl || !is_overloaded_fn (decl))
4069 return error_mark_node;
4070
4071 for (fns = decl; fns; fns = OVL_NEXT (fns))
4072 {
4073 unsigned int ix;
4074 bool found = true;
4075 tree fn = OVL_CURRENT (fns);
4076 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4077 if (parmtypes != NULL_TREE)
4078 {
4079 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4080 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4081 {
4082 tree tparm = TREE_VALUE (parmtypes);
4083 tree targ = TREE_TYPE ((*args)[ix]);
4084 bool ptr = TYPE_PTR_P (tparm);
4085 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4086 if ((ptr || arr || !same_type_p (tparm, targ))
4087 && (!ptr || !arr
4088 || !same_type_p (TREE_TYPE (tparm),
4089 TREE_TYPE (targ))))
4090 found = false;
4091 }
4092 if (found
4093 && ix == vec_safe_length (args)
4094 /* May be this should be sufficient_parms_p instead,
4095 depending on how exactly should user-defined literals
4096 work in presence of default arguments on the literal
4097 operator parameters. */
4098 && parmtypes == void_list_node)
4099 return decl;
4100 }
4101 }
4102
4103 return error_mark_node;
4104}
4105
4106/* Parse a user-defined char constant. Returns a call to a user-defined
4107 literal operator taking the character as an argument. */
4108
4109static cp_expr
4110cp_parser_userdef_char_literal (cp_parser *parser)
4111{
4112 cp_token *token = cp_lexer_consume_token (parser->lexer);
4113 tree literal = token->u.value;
4114 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4115 tree value = USERDEF_LITERAL_VALUE (literal);
4116 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4117 tree decl, result;
4118
4119 /* Build up a call to the user-defined operator */
4120 /* Lookup the name we got back from the id-expression. */
4121 vec<tree, va_gc> *args = make_tree_vector ();
4122 vec_safe_push (args, value);
4123 decl = lookup_literal_operator (name, args);
4124 if (!decl || decl == error_mark_node)
4125 {
4126 error ("unable to find character literal operator %qD with %qT argument",
4127 name, TREE_TYPE (value));
4128 release_tree_vector (args);
4129 return error_mark_node;
4130 }
4131 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4132 release_tree_vector (args);
4133 return result;
4134}
4135
4136/* A subroutine of cp_parser_userdef_numeric_literal to
4137 create a char... template parameter pack from a string node. */
4138
4139static tree
4140make_char_string_pack (tree value)
4141{
4142 tree charvec;
4143 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4144 const char *str = TREE_STRING_POINTER (value);
4145 int i, len = TREE_STRING_LENGTH (value) - 1;
4146 tree argvec = make_tree_vec (1);
4147
4148 /* Fill in CHARVEC with all of the parameters. */
4149 charvec = make_tree_vec (len);
4150 for (i = 0; i < len; ++i)
4151 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4152
4153 /* Build the argument packs. */
4154 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4155 TREE_TYPE (argpack) = char_type_node;
4156
4157 TREE_VEC_ELT (argvec, 0) = argpack;
4158
4159 return argvec;
4160}
4161
4162/* A subroutine of cp_parser_userdef_numeric_literal to
4163 create a char... template parameter pack from a string node. */
4164
4165static tree
4166make_string_pack (tree value)
4167{
4168 tree charvec;
4169 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4170 const unsigned char *str
4171 = (const unsigned char *) TREE_STRING_POINTER (value);
4172 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4173 int len = TREE_STRING_LENGTH (value) / sz - 1;
4174 tree argvec = make_tree_vec (2);
4175
4176 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4177 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4178
4179 /* First template parm is character type. */
4180 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4181
4182 /* Fill in CHARVEC with all of the parameters. */
4183 charvec = make_tree_vec (len);
4184 for (int i = 0; i < len; ++i)
4185 TREE_VEC_ELT (charvec, i)
4186 = double_int_to_tree (str_char_type_node,
4187 double_int::from_buffer (str + i * sz, sz));
4188
4189 /* Build the argument packs. */
4190 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4191 TREE_TYPE (argpack) = str_char_type_node;
4192
4193 TREE_VEC_ELT (argvec, 1) = argpack;
4194
4195 return argvec;
4196}
4197
4198/* Parse a user-defined numeric constant. returns a call to a user-defined
4199 literal operator. */
4200
4201static cp_expr
4202cp_parser_userdef_numeric_literal (cp_parser *parser)
4203{
4204 cp_token *token = cp_lexer_consume_token (parser->lexer);
4205 tree literal = token->u.value;
4206 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4207 tree value = USERDEF_LITERAL_VALUE (literal);
4208 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4209 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4210 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4211 tree decl, result;
4212 vec<tree, va_gc> *args;
4213
4214 /* Look for a literal operator taking the exact type of numeric argument
4215 as the literal value. */
4216 args = make_tree_vector ();
4217 vec_safe_push (args, value);
4218 decl = lookup_literal_operator (name, args);
4219 if (decl && decl != error_mark_node)
4220 {
4221 result = finish_call_expr (decl, &args, false, true,
4222 tf_warning_or_error);
4223
4224 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4225 {
4226 warning_at (token->location, OPT_Woverflow,
4227 "integer literal exceeds range of %qT type",
4228 long_long_unsigned_type_node);
4229 }
4230 else
4231 {
4232 if (overflow > 0)
4233 warning_at (token->location, OPT_Woverflow,
4234 "floating literal exceeds range of %qT type",
4235 long_double_type_node);
4236 else if (overflow < 0)
4237 warning_at (token->location, OPT_Woverflow,
4238 "floating literal truncated to zero");
4239 }
4240
4241 release_tree_vector (args);
4242 return result;
4243 }
4244 release_tree_vector (args);
4245
4246 /* If the numeric argument didn't work, look for a raw literal
4247 operator taking a const char* argument consisting of the number
4248 in string format. */
4249 args = make_tree_vector ();
4250 vec_safe_push (args, num_string);
4251 decl = lookup_literal_operator (name, args);
4252 if (decl && decl != error_mark_node)
4253 {
4254 result = finish_call_expr (decl, &args, false, true,
4255 tf_warning_or_error);
4256 release_tree_vector (args);
4257 return result;
4258 }
4259 release_tree_vector (args);
4260
4261 /* If the raw literal didn't work, look for a non-type template
4262 function with parameter pack char.... Call the function with
4263 template parameter characters representing the number. */
4264 args = make_tree_vector ();
4265 decl = lookup_literal_operator (name, args);
4266 if (decl && decl != error_mark_node)
4267 {
4268 tree tmpl_args = make_char_string_pack (num_string);
4269 decl = lookup_template_function (decl, tmpl_args);
4270 result = finish_call_expr (decl, &args, false, true,
4271 tf_warning_or_error);
4272 release_tree_vector (args);
4273 return result;
4274 }
4275
4276 release_tree_vector (args);
4277
4278 error ("unable to find numeric literal operator %qD", name);
4279 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4280 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4281 "to enable more built-in suffixes");
4282 return error_mark_node;
4283}
4284
4285/* Parse a user-defined string constant. Returns a call to a user-defined
4286 literal operator taking a character pointer and the length of the string
4287 as arguments. */
4288
4289static tree
4290cp_parser_userdef_string_literal (tree literal)
4291{
4292 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4293 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4294 tree value = USERDEF_LITERAL_VALUE (literal);
4295 int len = TREE_STRING_LENGTH (value)
4296 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4297 tree decl, result;
4298 vec<tree, va_gc> *args;
4299
4300 /* Build up a call to the user-defined operator. */
4301 /* Lookup the name we got back from the id-expression. */
4302 args = make_tree_vector ();
4303 vec_safe_push (args, value);
4304 vec_safe_push (args, build_int_cst (size_type_node, len));
4305 decl = lookup_literal_operator (name, args);
4306
4307 if (decl && decl != error_mark_node)
4308 {
4309 result = finish_call_expr (decl, &args, false, true,
4310 tf_warning_or_error);
4311 release_tree_vector (args);
4312 return result;
4313 }
4314 release_tree_vector (args);
4315
4316 /* Look for a template function with typename parameter CharT
4317 and parameter pack CharT... Call the function with
4318 template parameter characters representing the string. */
4319 args = make_tree_vector ();
4320 decl = lookup_literal_operator (name, args);
4321 if (decl && decl != error_mark_node)
4322 {
4323 tree tmpl_args = make_string_pack (value);
4324 decl = lookup_template_function (decl, tmpl_args);
4325 result = finish_call_expr (decl, &args, false, true,
4326 tf_warning_or_error);
4327 release_tree_vector (args);
4328 return result;
4329 }
4330 release_tree_vector (args);
4331
4332 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4333 name, TREE_TYPE (value), size_type_node);
4334 return error_mark_node;
4335}
4336
4337
4338/* Basic concepts [gram.basic] */
4339
4340/* Parse a translation-unit.
4341
4342 translation-unit:
4343 declaration-seq [opt]
4344
4345 Returns TRUE if all went well. */
4346
4347static bool
4348cp_parser_translation_unit (cp_parser* parser)
4349{
4350 /* The address of the first non-permanent object on the declarator
4351 obstack. */
4352 static void *declarator_obstack_base;
4353
4354 bool success;
4355
4356 /* Create the declarator obstack, if necessary. */
4357 if (!cp_error_declarator)
4358 {
4359 gcc_obstack_init (&declarator_obstack);
4360 /* Create the error declarator. */
4361 cp_error_declarator = make_declarator (cdk_error);
4362 /* Create the empty parameter list. */
4363 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4364 /* Remember where the base of the declarator obstack lies. */
4365 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4366 }
4367
4368 cp_parser_declaration_seq_opt (parser);
4369
4370 /* If there are no tokens left then all went well. */
4371 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4372 {
4373 /* Get rid of the token array; we don't need it any more. */
4374 cp_lexer_destroy (parser->lexer);
4375 parser->lexer = NULL;
4376
4377 /* This file might have been a context that's implicitly extern
4378 "C". If so, pop the lang context. (Only relevant for PCH.) */
4379 if (parser->implicit_extern_c)
4380 {
4381 pop_lang_context ();
4382 parser->implicit_extern_c = false;
4383 }
4384
4385 /* Finish up. */
4386 finish_translation_unit ();
4387
4388 success = true;
4389 }
4390 else
4391 {
4392 cp_parser_error (parser, "expected declaration");
4393 success = false;
4394 }
4395
4396 /* Make sure the declarator obstack was fully cleaned up. */
4397 gcc_assert (obstack_next_free (&declarator_obstack)
4398 == declarator_obstack_base);
4399
4400 /* All went well. */
4401 return success;
4402}
4403
4404/* Return the appropriate tsubst flags for parsing, possibly in N3276
4405 decltype context. */
4406
4407static inline tsubst_flags_t
4408complain_flags (bool decltype_p)
4409{
4410 tsubst_flags_t complain = tf_warning_or_error;
4411 if (decltype_p)
4412 complain |= tf_decltype;
4413 return complain;
4414}
4415
4416/* We're about to parse a collection of statements. If we're currently
4417 parsing tentatively, set up a firewall so that any nested
4418 cp_parser_commit_to_tentative_parse won't affect the current context. */
4419
4420static cp_token_position
4421cp_parser_start_tentative_firewall (cp_parser *parser)
4422{
4423 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4424 return 0;
4425
4426 cp_parser_parse_tentatively (parser);
4427 cp_parser_commit_to_topmost_tentative_parse (parser);
4428 return cp_lexer_token_position (parser->lexer, false);
4429}
4430
4431/* We've finished parsing the collection of statements. Wrap up the
4432 firewall and replace the relevant tokens with the parsed form. */
4433
4434static void
4435cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4436 tree expr)
4437{
4438 if (!start)
4439 return;
4440
4441 /* Finish the firewall level. */
4442 cp_parser_parse_definitely (parser);
4443 /* And remember the result of the parse for when we try again. */
4444 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4445 token->type = CPP_PREPARSED_EXPR;
4446 token->u.value = expr;
4447 token->keyword = RID_MAX;
4448 cp_lexer_purge_tokens_after (parser->lexer, start);
4449}
4450
4451/* Like the above functions, but let the user modify the tokens. Used by
4452 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4453 later parses, so it makes sense to localize the effects of
4454 cp_parser_commit_to_tentative_parse. */
4455
4456struct tentative_firewall
4457{
4458 cp_parser *parser;
4459 bool set;
4460
4461 tentative_firewall (cp_parser *p): parser(p)
4462 {
4463 /* If we're currently parsing tentatively, start a committed level as a
4464 firewall and then an inner tentative parse. */
4465 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4466 {
4467 cp_parser_parse_tentatively (parser);
4468 cp_parser_commit_to_topmost_tentative_parse (parser);
4469 cp_parser_parse_tentatively (parser);
4470 }
4471 }
4472
4473 ~tentative_firewall()
4474 {
4475 if (set)
4476 {
4477 /* Finish the inner tentative parse and the firewall, propagating any
4478 uncommitted error state to the outer tentative parse. */
4479 bool err = cp_parser_error_occurred (parser);
4480 cp_parser_parse_definitely (parser);
4481 cp_parser_parse_definitely (parser);
4482 if (err)
4483 cp_parser_simulate_error (parser);
4484 }
4485 }
4486};
4487
4488/* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4489 enclosing parentheses. */
4490
4491static cp_expr
4492cp_parser_statement_expr (cp_parser *parser)
4493{
4494 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4495
4496 /* Consume the '('. */
4497 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4498 cp_lexer_consume_token (parser->lexer);
4499 /* Start the statement-expression. */
4500 tree expr = begin_stmt_expr ();
4501 /* Parse the compound-statement. */
4502 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4503 /* Finish up. */
4504 expr = finish_stmt_expr (expr, false);
4505 /* Consume the ')'. */
4506 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4507 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4508 cp_parser_skip_to_end_of_statement (parser);
4509
4510 cp_parser_end_tentative_firewall (parser, start, expr);
4511 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4512 return cp_expr (expr, combined_loc);
4513}
4514
4515/* Expressions [gram.expr] */
4516
4517/* Parse a fold-operator.
4518
4519 fold-operator:
4520 - * / % ^ & | = < > << >>
4521 = -= *= /= %= ^= &= |= <<= >>=
4522 == != <= >= && || , .* ->*
4523
4524 This returns the tree code corresponding to the matched operator
4525 as an int. When the current token matches a compound assignment
4526 opertor, the resulting tree code is the negative value of the
4527 non-assignment operator. */
4528
4529static int
4530cp_parser_fold_operator (cp_token *token)
4531{
4532 switch (token->type)
4533 {
4534 case CPP_PLUS: return PLUS_EXPR;
4535 case CPP_MINUS: return MINUS_EXPR;
4536 case CPP_MULT: return MULT_EXPR;
4537 case CPP_DIV: return TRUNC_DIV_EXPR;
4538 case CPP_MOD: return TRUNC_MOD_EXPR;
4539 case CPP_XOR: return BIT_XOR_EXPR;
4540 case CPP_AND: return BIT_AND_EXPR;
4541 case CPP_OR: return BIT_IOR_EXPR;
4542 case CPP_LSHIFT: return LSHIFT_EXPR;
4543 case CPP_RSHIFT: return RSHIFT_EXPR;
4544
4545 case CPP_EQ: return -NOP_EXPR;
4546 case CPP_PLUS_EQ: return -PLUS_EXPR;
4547 case CPP_MINUS_EQ: return -MINUS_EXPR;
4548 case CPP_MULT_EQ: return -MULT_EXPR;
4549 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4550 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4551 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4552 case CPP_AND_EQ: return -BIT_AND_EXPR;
4553 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4554 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4555 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4556
4557 case CPP_EQ_EQ: return EQ_EXPR;
4558 case CPP_NOT_EQ: return NE_EXPR;
4559 case CPP_LESS: return LT_EXPR;
4560 case CPP_GREATER: return GT_EXPR;
4561 case CPP_LESS_EQ: return LE_EXPR;
4562 case CPP_GREATER_EQ: return GE_EXPR;
4563
4564 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4565 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4566
4567 case CPP_COMMA: return COMPOUND_EXPR;
4568
4569 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4570 case CPP_DEREF_STAR: return MEMBER_REF;
4571
4572 default: return ERROR_MARK;
4573 }
4574}
4575
4576/* Returns true if CODE indicates a binary expression, which is not allowed in
4577 the LHS of a fold-expression. More codes will need to be added to use this
4578 function in other contexts. */
4579
4580static bool
4581is_binary_op (tree_code code)
4582{
4583 switch (code)
4584 {
4585 case PLUS_EXPR:
4586 case POINTER_PLUS_EXPR:
4587 case MINUS_EXPR:
4588 case MULT_EXPR:
4589 case TRUNC_DIV_EXPR:
4590 case TRUNC_MOD_EXPR:
4591 case BIT_XOR_EXPR:
4592 case BIT_AND_EXPR:
4593 case BIT_IOR_EXPR:
4594 case LSHIFT_EXPR:
4595 case RSHIFT_EXPR:
4596
4597 case MODOP_EXPR:
4598
4599 case EQ_EXPR:
4600 case NE_EXPR:
4601 case LE_EXPR:
4602 case GE_EXPR:
4603 case LT_EXPR:
4604 case GT_EXPR:
4605
4606 case TRUTH_ANDIF_EXPR:
4607 case TRUTH_ORIF_EXPR:
4608
4609 case COMPOUND_EXPR:
4610
4611 case DOTSTAR_EXPR:
4612 case MEMBER_REF:
4613 return true;
4614
4615 default:
4616 return false;
4617 }
4618}
4619
4620/* If the next token is a suitable fold operator, consume it and return as
4621 the function above. */
4622
4623static int
4624cp_parser_fold_operator (cp_parser *parser)
4625{
4626 cp_token* token = cp_lexer_peek_token (parser->lexer);
4627 int code = cp_parser_fold_operator (token);
4628 if (code != ERROR_MARK)
4629 cp_lexer_consume_token (parser->lexer);
4630 return code;
4631}
4632
4633/* Parse a fold-expression.
4634
4635 fold-expression:
4636 ( ... folding-operator cast-expression)
4637 ( cast-expression folding-operator ... )
4638 ( cast-expression folding operator ... folding-operator cast-expression)
4639
4640 Note that the '(' and ')' are matched in primary expression. */
4641
4642static cp_expr
4643cp_parser_fold_expression (cp_parser *parser, tree expr1)
4644{
4645 cp_id_kind pidk;
4646
4647 // Left fold.
4648 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4649 {
4650 cp_lexer_consume_token (parser->lexer);
4651 int op = cp_parser_fold_operator (parser);
4652 if (op == ERROR_MARK)
4653 {
4654 cp_parser_error (parser, "expected binary operator");
4655 return error_mark_node;
4656 }
4657
4658 tree expr = cp_parser_cast_expression (parser, false, false,
4659 false, &pidk);
4660 if (expr == error_mark_node)
4661 return error_mark_node;
4662 return finish_left_unary_fold_expr (expr, op);
4663 }
4664
4665 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4666 int op = cp_parser_fold_operator (parser);
4667 if (op == ERROR_MARK)
4668 {
4669 cp_parser_error (parser, "expected binary operator");
4670 return error_mark_node;
4671 }
4672
4673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4674 {
4675 cp_parser_error (parser, "expected ...");
4676 return error_mark_node;
4677 }
4678 cp_lexer_consume_token (parser->lexer);
4679
4680 /* The operands of a fold-expression are cast-expressions, so binary or
4681 conditional expressions are not allowed. We check this here to avoid
4682 tentative parsing. */
4683 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4684 /* OK, the expression was parenthesized. */;
4685 else if (is_binary_op (TREE_CODE (expr1)))
4686 error_at (location_of (expr1),
4687 "binary expression in operand of fold-expression");
4688 else if (TREE_CODE (expr1) == COND_EXPR)
4689 error_at (location_of (expr1),
4690 "conditional expression in operand of fold-expression");
4691
4692 // Right fold.
4693 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4694 return finish_right_unary_fold_expr (expr1, op);
4695
4696 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4697 {
4698 cp_parser_error (parser, "mismatched operator in fold-expression");
4699 return error_mark_node;
4700 }
4701 cp_lexer_consume_token (parser->lexer);
4702
4703 // Binary left or right fold.
4704 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4705 if (expr2 == error_mark_node)
4706 return error_mark_node;
4707 return finish_binary_fold_expr (expr1, expr2, op);
4708}
4709
4710/* Parse a primary-expression.
4711
4712 primary-expression:
4713 literal
4714 this
4715 ( expression )
4716 id-expression
4717 lambda-expression (C++11)
4718
4719 GNU Extensions:
4720
4721 primary-expression:
4722 ( compound-statement )
4723 __builtin_va_arg ( assignment-expression , type-id )
4724 __builtin_offsetof ( type-id , offsetof-expression )
4725
4726 C++ Extensions:
4727 __has_nothrow_assign ( type-id )
4728 __has_nothrow_constructor ( type-id )
4729 __has_nothrow_copy ( type-id )
4730 __has_trivial_assign ( type-id )
4731 __has_trivial_constructor ( type-id )
4732 __has_trivial_copy ( type-id )
4733 __has_trivial_destructor ( type-id )
4734 __has_virtual_destructor ( type-id )
4735 __is_abstract ( type-id )
4736 __is_base_of ( type-id , type-id )
4737 __is_class ( type-id )
4738 __is_empty ( type-id )
4739 __is_enum ( type-id )
4740 __is_final ( type-id )
4741 __is_literal_type ( type-id )
4742 __is_pod ( type-id )
4743 __is_polymorphic ( type-id )
4744 __is_std_layout ( type-id )
4745 __is_trivial ( type-id )
4746 __is_union ( type-id )
4747
4748 Objective-C++ Extension:
4749
4750 primary-expression:
4751 objc-expression
4752
4753 literal:
4754 __null
4755
4756 ADDRESS_P is true iff this expression was immediately preceded by
4757 "&" and therefore might denote a pointer-to-member. CAST_P is true
4758 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4759 true iff this expression is a template argument.
4760
4761 Returns a representation of the expression. Upon return, *IDK
4762 indicates what kind of id-expression (if any) was present. */
4763
4764static cp_expr
4765cp_parser_primary_expression (cp_parser *parser,
4766 bool address_p,
4767 bool cast_p,
4768 bool template_arg_p,
4769 bool decltype_p,
4770 cp_id_kind *idk)
4771{
4772 cp_token *token = NULL;
4773
4774 /* Assume the primary expression is not an id-expression. */
4775 *idk = CP_ID_KIND_NONE;
4776
4777 /* Peek at the next token. */
4778 token = cp_lexer_peek_token (parser->lexer);
4779 switch ((int) token->type)
4780 {
4781 /* literal:
4782 integer-literal
4783 character-literal
4784 floating-literal
4785 string-literal
4786 boolean-literal
4787 pointer-literal
4788 user-defined-literal */
4789 case CPP_CHAR:
4790 case CPP_CHAR16:
4791 case CPP_CHAR32:
4792 case CPP_WCHAR:
4793 case CPP_UTF8CHAR:
4794 case CPP_NUMBER:
4795 case CPP_PREPARSED_EXPR:
4796 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4797 return cp_parser_userdef_numeric_literal (parser);
4798 token = cp_lexer_consume_token (parser->lexer);
4799 if (TREE_CODE (token->u.value) == FIXED_CST)
4800 {
4801 error_at (token->location,
4802 "fixed-point types not supported in C++");
4803 return error_mark_node;
4804 }
4805 /* Floating-point literals are only allowed in an integral
4806 constant expression if they are cast to an integral or
4807 enumeration type. */
4808 if (TREE_CODE (token->u.value) == REAL_CST
4809 && parser->integral_constant_expression_p
4810 && pedantic)
4811 {
4812 /* CAST_P will be set even in invalid code like "int(2.7 +
4813 ...)". Therefore, we have to check that the next token
4814 is sure to end the cast. */
4815 if (cast_p)
4816 {
4817 cp_token *next_token;
4818
4819 next_token = cp_lexer_peek_token (parser->lexer);
4820 if (/* The comma at the end of an
4821 enumerator-definition. */
4822 next_token->type != CPP_COMMA
4823 /* The curly brace at the end of an enum-specifier. */
4824 && next_token->type != CPP_CLOSE_BRACE
4825 /* The end of a statement. */
4826 && next_token->type != CPP_SEMICOLON
4827 /* The end of the cast-expression. */
4828 && next_token->type != CPP_CLOSE_PAREN
4829 /* The end of an array bound. */
4830 && next_token->type != CPP_CLOSE_SQUARE
4831 /* The closing ">" in a template-argument-list. */
4832 && (next_token->type != CPP_GREATER
4833 || parser->greater_than_is_operator_p)
4834 /* C++0x only: A ">>" treated like two ">" tokens,
4835 in a template-argument-list. */
4836 && (next_token->type != CPP_RSHIFT
4837 || (cxx_dialect == cxx98)
4838 || parser->greater_than_is_operator_p))
4839 cast_p = false;
4840 }
4841
4842 /* If we are within a cast, then the constraint that the
4843 cast is to an integral or enumeration type will be
4844 checked at that point. If we are not within a cast, then
4845 this code is invalid. */
4846 if (!cast_p)
4847 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4848 }
4849 return cp_expr (token->u.value, token->location);
4850
4851 case CPP_CHAR_USERDEF:
4852 case CPP_CHAR16_USERDEF:
4853 case CPP_CHAR32_USERDEF:
4854 case CPP_WCHAR_USERDEF:
4855 case CPP_UTF8CHAR_USERDEF:
4856 return cp_parser_userdef_char_literal (parser);
4857
4858 case CPP_STRING:
4859 case CPP_STRING16:
4860 case CPP_STRING32:
4861 case CPP_WSTRING:
4862 case CPP_UTF8STRING:
4863 case CPP_STRING_USERDEF:
4864 case CPP_STRING16_USERDEF:
4865 case CPP_STRING32_USERDEF:
4866 case CPP_WSTRING_USERDEF:
4867 case CPP_UTF8STRING_USERDEF:
4868 /* ??? Should wide strings be allowed when parser->translate_strings_p
4869 is false (i.e. in attributes)? If not, we can kill the third
4870 argument to cp_parser_string_literal. */
4871 return cp_parser_string_literal (parser,
4872 parser->translate_strings_p,
4873 true);
4874
4875 case CPP_OPEN_PAREN:
4876 /* If we see `( { ' then we are looking at the beginning of
4877 a GNU statement-expression. */
4878 if (cp_parser_allow_gnu_extensions_p (parser)
4879 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4880 {
4881 /* Statement-expressions are not allowed by the standard. */
4882 pedwarn (token->location, OPT_Wpedantic,
4883 "ISO C++ forbids braced-groups within expressions");
4884
4885 /* And they're not allowed outside of a function-body; you
4886 cannot, for example, write:
4887
4888 int i = ({ int j = 3; j + 1; });
4889
4890 at class or namespace scope. */
4891 if (!parser->in_function_body
4892 || parser->in_template_argument_list_p)
4893 {
4894 error_at (token->location,
4895 "statement-expressions are not allowed outside "
4896 "functions nor in template-argument lists");
4897 cp_parser_skip_to_end_of_block_or_statement (parser);
4898 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4899 cp_lexer_consume_token (parser->lexer);
4900 return error_mark_node;
4901 }
4902 else
4903 return cp_parser_statement_expr (parser);
4904 }
4905 /* Otherwise it's a normal parenthesized expression. */
4906 {
4907 cp_expr expr;
4908 bool saved_greater_than_is_operator_p;
4909
4910 location_t open_paren_loc = token->location;
4911
4912 /* Consume the `('. */
4913 cp_lexer_consume_token (parser->lexer);
4914 /* Within a parenthesized expression, a `>' token is always
4915 the greater-than operator. */
4916 saved_greater_than_is_operator_p
4917 = parser->greater_than_is_operator_p;
4918 parser->greater_than_is_operator_p = true;
4919
4920 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4921 /* Left fold expression. */
4922 expr = NULL_TREE;
4923 else
4924 /* Parse the parenthesized expression. */
4925 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4926
4927 token = cp_lexer_peek_token (parser->lexer);
4928 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4929 {
4930 expr = cp_parser_fold_expression (parser, expr);
4931 if (expr != error_mark_node
4932 && cxx_dialect < cxx1z
4933 && !in_system_header_at (input_location))
4934 pedwarn (input_location, 0, "fold-expressions only available "
4935 "with -std=c++1z or -std=gnu++1z");
4936 }
4937 else
4938 /* Let the front end know that this expression was
4939 enclosed in parentheses. This matters in case, for
4940 example, the expression is of the form `A::B', since
4941 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4942 not. */
4943 expr = finish_parenthesized_expr (expr);
4944
4945 /* DR 705: Wrapping an unqualified name in parentheses
4946 suppresses arg-dependent lookup. We want to pass back
4947 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4948 (c++/37862), but none of the others. */
4949 if (*idk != CP_ID_KIND_QUALIFIED)
4950 *idk = CP_ID_KIND_NONE;
4951
4952 /* The `>' token might be the end of a template-id or
4953 template-parameter-list now. */
4954 parser->greater_than_is_operator_p
4955 = saved_greater_than_is_operator_p;
4956
4957 /* Consume the `)'. */
4958 token = cp_lexer_peek_token (parser->lexer);
4959 location_t close_paren_loc = token->location;
4960 expr.set_range (open_paren_loc, close_paren_loc);
4961 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4962 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4963 cp_parser_skip_to_end_of_statement (parser);
4964
4965 return expr;
4966 }
4967
4968 case CPP_OPEN_SQUARE:
4969 {
4970 if (c_dialect_objc ())
4971 {
4972 /* We might have an Objective-C++ message. */
4973 cp_parser_parse_tentatively (parser);
4974 tree msg = cp_parser_objc_message_expression (parser);
4975 /* If that works out, we're done ... */
4976 if (cp_parser_parse_definitely (parser))
4977 return msg;
4978 /* ... else, fall though to see if it's a lambda. */
4979 }
4980 cp_expr lam = cp_parser_lambda_expression (parser);
4981 /* Don't warn about a failed tentative parse. */
4982 if (cp_parser_error_occurred (parser))
4983 return error_mark_node;
4984 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4985 return lam;
4986 }
4987
4988 case CPP_OBJC_STRING:
4989 if (c_dialect_objc ())
4990 /* We have an Objective-C++ string literal. */
4991 return cp_parser_objc_expression (parser);
4992 cp_parser_error (parser, "expected primary-expression");
4993 return error_mark_node;
4994
4995 case CPP_KEYWORD:
4996 switch (token->keyword)
4997 {
4998 /* These two are the boolean literals. */
4999 case RID_TRUE:
5000 cp_lexer_consume_token (parser->lexer);
5001 return cp_expr (boolean_true_node, token->location);
5002 case RID_FALSE:
5003 cp_lexer_consume_token (parser->lexer);
5004 return cp_expr (boolean_false_node, token->location);
5005
5006 /* The `__null' literal. */
5007 case RID_NULL:
5008 cp_lexer_consume_token (parser->lexer);
5009 return cp_expr (null_node, token->location);
5010
5011 /* The `nullptr' literal. */
5012 case RID_NULLPTR:
5013 cp_lexer_consume_token (parser->lexer);
5014 return cp_expr (nullptr_node, token->location);
5015
5016 /* Recognize the `this' keyword. */
5017 case RID_THIS:
5018 cp_lexer_consume_token (parser->lexer);
5019 if (parser->local_variables_forbidden_p)
5020 {
5021 error_at (token->location,
5022 "%<this%> may not be used in this context");
5023 return error_mark_node;
5024 }
5025 /* Pointers cannot appear in constant-expressions. */
5026 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5027 return error_mark_node;
5028 return cp_expr (finish_this_expr (), token->location);
5029
5030 /* The `operator' keyword can be the beginning of an
5031 id-expression. */
5032 case RID_OPERATOR:
5033 goto id_expression;
5034
5035 case RID_FUNCTION_NAME:
5036 case RID_PRETTY_FUNCTION_NAME:
5037 case RID_C99_FUNCTION_NAME:
5038 {
5039 non_integral_constant name;
5040
5041 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5042 __func__ are the names of variables -- but they are
5043 treated specially. Therefore, they are handled here,
5044 rather than relying on the generic id-expression logic
5045 below. Grammatically, these names are id-expressions.
5046
5047 Consume the token. */
5048 token = cp_lexer_consume_token (parser->lexer);
5049
5050 switch (token->keyword)
5051 {
5052 case RID_FUNCTION_NAME:
5053 name = NIC_FUNC_NAME;
5054 break;
5055 case RID_PRETTY_FUNCTION_NAME:
5056 name = NIC_PRETTY_FUNC;
5057 break;
5058 case RID_C99_FUNCTION_NAME:
5059 name = NIC_C99_FUNC;
5060 break;
5061 default:
5062 gcc_unreachable ();
5063 }
5064
5065 if (cp_parser_non_integral_constant_expression (parser, name))
5066 return error_mark_node;
5067
5068 /* Look up the name. */
5069 return finish_fname (token->u.value);
5070 }
5071
5072 case RID_VA_ARG:
5073 {
5074 tree expression;
5075 tree type;
5076 source_location type_location;
5077 location_t start_loc
5078 = cp_lexer_peek_token (parser->lexer)->location;
5079 /* The `__builtin_va_arg' construct is used to handle
5080 `va_arg'. Consume the `__builtin_va_arg' token. */
5081 cp_lexer_consume_token (parser->lexer);
5082 /* Look for the opening `('. */
5083 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5084 /* Now, parse the assignment-expression. */
5085 expression = cp_parser_assignment_expression (parser);
5086 /* Look for the `,'. */
5087 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5088 type_location = cp_lexer_peek_token (parser->lexer)->location;
5089 /* Parse the type-id. */
5090 {
5091 type_id_in_expr_sentinel s (parser);
5092 type = cp_parser_type_id (parser);
5093 }
5094 /* Look for the closing `)'. */
5095 location_t finish_loc
5096 = cp_lexer_peek_token (parser->lexer)->location;
5097 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5098 /* Using `va_arg' in a constant-expression is not
5099 allowed. */
5100 if (cp_parser_non_integral_constant_expression (parser,
5101 NIC_VA_ARG))
5102 return error_mark_node;
5103 /* Construct a location of the form:
5104 __builtin_va_arg (v, int)
5105 ~~~~~~~~~~~~~~~~~~~~~^~~~
5106 with the caret at the type, ranging from the start of the
5107 "__builtin_va_arg" token to the close paren. */
5108 location_t combined_loc
5109 = make_location (type_location, start_loc, finish_loc);
5110 return build_x_va_arg (combined_loc, expression, type);
5111 }
5112
5113 case RID_OFFSETOF:
5114 return cp_parser_builtin_offsetof (parser);
5115
5116 case RID_HAS_NOTHROW_ASSIGN:
5117 case RID_HAS_NOTHROW_CONSTRUCTOR:
5118 case RID_HAS_NOTHROW_COPY:
5119 case RID_HAS_TRIVIAL_ASSIGN:
5120 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5121 case RID_HAS_TRIVIAL_COPY:
5122 case RID_HAS_TRIVIAL_DESTRUCTOR:
5123 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5124 case RID_HAS_VIRTUAL_DESTRUCTOR:
5125 case RID_IS_ABSTRACT:
5126 case RID_IS_AGGREGATE:
5127 case RID_IS_BASE_OF:
5128 case RID_IS_CLASS:
5129 case RID_IS_EMPTY:
5130 case RID_IS_ENUM:
5131 case RID_IS_FINAL:
5132 case RID_IS_LITERAL_TYPE:
5133 case RID_IS_POD:
5134 case RID_IS_POLYMORPHIC:
5135 case RID_IS_SAME_AS:
5136 case RID_IS_STD_LAYOUT:
5137 case RID_IS_TRIVIAL:
5138 case RID_IS_TRIVIALLY_ASSIGNABLE:
5139 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5140 case RID_IS_TRIVIALLY_COPYABLE:
5141 case RID_IS_UNION:
5142 return cp_parser_trait_expr (parser, token->keyword);
5143
5144 // C++ concepts
5145 case RID_REQUIRES:
5146 return cp_parser_requires_expression (parser);
5147
5148 /* Objective-C++ expressions. */
5149 case RID_AT_ENCODE:
5150 case RID_AT_PROTOCOL:
5151 case RID_AT_SELECTOR:
5152 return cp_parser_objc_expression (parser);
5153
5154 case RID_TEMPLATE:
5155 if (parser->in_function_body
5156 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5157 == CPP_LESS))
5158 {
5159 error_at (token->location,
5160 "a template declaration cannot appear at block scope");
5161 cp_parser_skip_to_end_of_block_or_statement (parser);
5162 return error_mark_node;
5163 }
5164 /* FALLTHRU */
5165 default:
5166 cp_parser_error (parser, "expected primary-expression");
5167 return error_mark_node;
5168 }
5169
5170 /* An id-expression can start with either an identifier, a
5171 `::' as the beginning of a qualified-id, or the "operator"
5172 keyword. */
5173 case CPP_NAME:
5174 case CPP_SCOPE:
5175 case CPP_TEMPLATE_ID:
5176 case CPP_NESTED_NAME_SPECIFIER:
5177 {
5178 id_expression:
5179 cp_expr id_expression;
5180 cp_expr decl;
5181 const char *error_msg;
5182 bool template_p;
5183 bool done;
5184 cp_token *id_expr_token;
5185
5186 /* Parse the id-expression. */
5187 id_expression
5188 = cp_parser_id_expression (parser,
5189 /*template_keyword_p=*/false,
5190 /*check_dependency_p=*/true,
5191 &template_p,
5192 /*declarator_p=*/false,
5193 /*optional_p=*/false);
5194 if (id_expression == error_mark_node)
5195 return error_mark_node;
5196 id_expr_token = token;
5197 token = cp_lexer_peek_token (parser->lexer);
5198 done = (token->type != CPP_OPEN_SQUARE
5199 && token->type != CPP_OPEN_PAREN
5200 && token->type != CPP_DOT
5201 && token->type != CPP_DEREF
5202 && token->type != CPP_PLUS_PLUS
5203 && token->type != CPP_MINUS_MINUS);
5204 /* If we have a template-id, then no further lookup is
5205 required. If the template-id was for a template-class, we
5206 will sometimes have a TYPE_DECL at this point. */
5207 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5208 || TREE_CODE (id_expression) == TYPE_DECL)
5209 decl = id_expression;
5210 /* Look up the name. */
5211 else
5212 {
5213 tree ambiguous_decls;
5214
5215 /* If we already know that this lookup is ambiguous, then
5216 we've already issued an error message; there's no reason
5217 to check again. */
5218 if (id_expr_token->type == CPP_NAME
5219 && id_expr_token->error_reported)
5220 {
5221 cp_parser_simulate_error (parser);
5222 return error_mark_node;
5223 }
5224
5225 decl = cp_parser_lookup_name (parser, id_expression,
5226 none_type,
5227 template_p,
5228 /*is_namespace=*/false,
5229 /*check_dependency=*/true,
5230 &ambiguous_decls,
5231 id_expr_token->location);
5232 /* If the lookup was ambiguous, an error will already have
5233 been issued. */
5234 if (ambiguous_decls)
5235 return error_mark_node;
5236
5237 /* In Objective-C++, we may have an Objective-C 2.0
5238 dot-syntax for classes here. */
5239 if (c_dialect_objc ()
5240 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5241 && TREE_CODE (decl) == TYPE_DECL
5242 && objc_is_class_name (decl))
5243 {
5244 tree component;
5245 cp_lexer_consume_token (parser->lexer);
5246 component = cp_parser_identifier (parser);
5247 if (component == error_mark_node)
5248 return error_mark_node;
5249
5250 tree result = objc_build_class_component_ref (id_expression,
5251 component);
5252 /* Build a location of the form:
5253 expr.component
5254 ~~~~~^~~~~~~~~
5255 with caret at the start of the component name (at
5256 input_location), ranging from the start of the id_expression
5257 to the end of the component name. */
5258 location_t combined_loc
5259 = make_location (input_location, id_expression.get_start (),
5260 get_finish (input_location));
5261 protected_set_expr_location (result, combined_loc);
5262 return result;
5263 }
5264
5265 /* In Objective-C++, an instance variable (ivar) may be preferred
5266 to whatever cp_parser_lookup_name() found.
5267 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5268 rest of c-family, we have to do a little extra work to preserve
5269 any location information in cp_expr "decl". Given that
5270 objc_lookup_ivar is implemented in "c-family" and "objc", we
5271 have a trip through the pure "tree" type, rather than cp_expr.
5272 Naively copying it back to "decl" would implicitly give the
5273 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5274 store an EXPR_LOCATION. Hence we only update "decl" (and
5275 hence its location_t) if we get back a different tree node. */
5276 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5277 id_expression);
5278 if (decl_tree != decl.get_value ())
5279 decl = cp_expr (decl_tree);
5280
5281 /* If name lookup gives us a SCOPE_REF, then the
5282 qualifying scope was dependent. */
5283 if (TREE_CODE (decl) == SCOPE_REF)
5284 {
5285 /* At this point, we do not know if DECL is a valid
5286 integral constant expression. We assume that it is
5287 in fact such an expression, so that code like:
5288
5289 template <int N> struct A {
5290 int a[B<N>::i];
5291 };
5292
5293 is accepted. At template-instantiation time, we
5294 will check that B<N>::i is actually a constant. */
5295 return decl;
5296 }
5297 /* Check to see if DECL is a local variable in a context
5298 where that is forbidden. */
5299 if (parser->local_variables_forbidden_p
5300 && local_variable_p (decl))
5301 {
5302 /* It might be that we only found DECL because we are
5303 trying to be generous with pre-ISO scoping rules.
5304 For example, consider:
5305
5306 int i;
5307 void g() {
5308 for (int i = 0; i < 10; ++i) {}
5309 extern void f(int j = i);
5310 }
5311
5312 Here, name look up will originally find the out
5313 of scope `i'. We need to issue a warning message,
5314 but then use the global `i'. */
5315 decl = check_for_out_of_scope_variable (decl);
5316 if (local_variable_p (decl))
5317 {
5318 error_at (id_expr_token->location,
5319 "local variable %qD may not appear in this context",
5320 decl.get_value ());
5321 return error_mark_node;
5322 }
5323 }
5324 }
5325
5326 decl = (finish_id_expression
5327 (id_expression, decl, parser->scope,
5328 idk,
5329 parser->integral_constant_expression_p,
5330 parser->allow_non_integral_constant_expression_p,
5331 &parser->non_integral_constant_expression_p,
5332 template_p, done, address_p,
5333 template_arg_p,
5334 &error_msg,
5335 id_expression.get_location ()));
5336 if (error_msg)
5337 cp_parser_error (parser, error_msg);
5338 decl.set_location (id_expr_token->location);
5339 return decl;
5340 }
5341
5342 /* Anything else is an error. */
5343 default:
5344 cp_parser_error (parser, "expected primary-expression");
5345 return error_mark_node;
5346 }
5347}
5348
5349static inline cp_expr
5350cp_parser_primary_expression (cp_parser *parser,
5351 bool address_p,
5352 bool cast_p,
5353 bool template_arg_p,
5354 cp_id_kind *idk)
5355{
5356 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5357 /*decltype*/false, idk);
5358}
5359
5360/* Parse an id-expression.
5361
5362 id-expression:
5363 unqualified-id
5364 qualified-id
5365
5366 qualified-id:
5367 :: [opt] nested-name-specifier template [opt] unqualified-id
5368 :: identifier
5369 :: operator-function-id
5370 :: template-id
5371
5372 Return a representation of the unqualified portion of the
5373 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5374 a `::' or nested-name-specifier.
5375
5376 Often, if the id-expression was a qualified-id, the caller will
5377 want to make a SCOPE_REF to represent the qualified-id. This
5378 function does not do this in order to avoid wastefully creating
5379 SCOPE_REFs when they are not required.
5380
5381 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5382 `template' keyword.
5383
5384 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5385 uninstantiated templates.
5386
5387 If *TEMPLATE_P is non-NULL, it is set to true iff the
5388 `template' keyword is used to explicitly indicate that the entity
5389 named is a template.
5390
5391 If DECLARATOR_P is true, the id-expression is appearing as part of
5392 a declarator, rather than as part of an expression. */
5393
5394static cp_expr
5395cp_parser_id_expression (cp_parser *parser,
5396 bool template_keyword_p,
5397 bool check_dependency_p,
5398 bool *template_p,
5399 bool declarator_p,
5400 bool optional_p)
5401{
5402 bool global_scope_p;
5403 bool nested_name_specifier_p;
5404
5405 /* Assume the `template' keyword was not used. */
5406 if (template_p)
5407 *template_p = template_keyword_p;
5408
5409 /* Look for the optional `::' operator. */
5410 global_scope_p
5411 = (!template_keyword_p
5412 && (cp_parser_global_scope_opt (parser,
5413 /*current_scope_valid_p=*/false)
5414 != NULL_TREE));
5415
5416 /* Look for the optional nested-name-specifier. */
5417 nested_name_specifier_p
5418 = (cp_parser_nested_name_specifier_opt (parser,
5419 /*typename_keyword_p=*/false,
5420 check_dependency_p,
5421 /*type_p=*/false,
5422 declarator_p,
5423 template_keyword_p)
5424 != NULL_TREE);
5425
5426 /* If there is a nested-name-specifier, then we are looking at
5427 the first qualified-id production. */
5428 if (nested_name_specifier_p)
5429 {
5430 tree saved_scope;
5431 tree saved_object_scope;
5432 tree saved_qualifying_scope;
5433 cp_expr unqualified_id;
5434 bool is_template;
5435
5436 /* See if the next token is the `template' keyword. */
5437 if (!template_p)
5438 template_p = &is_template;
5439 *template_p = cp_parser_optional_template_keyword (parser);
5440 /* Name lookup we do during the processing of the
5441 unqualified-id might obliterate SCOPE. */
5442 saved_scope = parser->scope;
5443 saved_object_scope = parser->object_scope;
5444 saved_qualifying_scope = parser->qualifying_scope;
5445 /* Process the final unqualified-id. */
5446 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5447 check_dependency_p,
5448 declarator_p,
5449 /*optional_p=*/false);
5450 /* Restore the SAVED_SCOPE for our caller. */
5451 parser->scope = saved_scope;
5452 parser->object_scope = saved_object_scope;
5453 parser->qualifying_scope = saved_qualifying_scope;
5454
5455 return unqualified_id;
5456 }
5457 /* Otherwise, if we are in global scope, then we are looking at one
5458 of the other qualified-id productions. */
5459 else if (global_scope_p)
5460 {
5461 cp_token *token;
5462 tree id;
5463
5464 /* Peek at the next token. */
5465 token = cp_lexer_peek_token (parser->lexer);
5466
5467 /* If it's an identifier, and the next token is not a "<", then
5468 we can avoid the template-id case. This is an optimization
5469 for this common case. */
5470 if (token->type == CPP_NAME
5471 && !cp_parser_nth_token_starts_template_argument_list_p
5472 (parser, 2))
5473 return cp_parser_identifier (parser);
5474
5475 cp_parser_parse_tentatively (parser);
5476 /* Try a template-id. */
5477 id = cp_parser_template_id (parser,
5478 /*template_keyword_p=*/false,
5479 /*check_dependency_p=*/true,
5480 none_type,
5481 declarator_p);
5482 /* If that worked, we're done. */
5483 if (cp_parser_parse_definitely (parser))
5484 return id;
5485
5486 /* Peek at the next token. (Changes in the token buffer may
5487 have invalidated the pointer obtained above.) */
5488 token = cp_lexer_peek_token (parser->lexer);
5489
5490 switch (token->type)
5491 {
5492 case CPP_NAME:
5493 return cp_parser_identifier (parser);
5494
5495 case CPP_KEYWORD:
5496 if (token->keyword == RID_OPERATOR)
5497 return cp_parser_operator_function_id (parser);
5498 /* Fall through. */
5499
5500 default:
5501 cp_parser_error (parser, "expected id-expression");
5502 return error_mark_node;
5503 }
5504 }
5505 else
5506 return cp_parser_unqualified_id (parser, template_keyword_p,
5507 /*check_dependency_p=*/true,
5508 declarator_p,
5509 optional_p);
5510}
5511
5512/* Parse an unqualified-id.
5513
5514 unqualified-id:
5515 identifier
5516 operator-function-id
5517 conversion-function-id
5518 ~ class-name
5519 template-id
5520
5521 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5522 keyword, in a construct like `A::template ...'.
5523
5524 Returns a representation of unqualified-id. For the `identifier'
5525 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5526 production a BIT_NOT_EXPR is returned; the operand of the
5527 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5528 other productions, see the documentation accompanying the
5529 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5530 names are looked up in uninstantiated templates. If DECLARATOR_P
5531 is true, the unqualified-id is appearing as part of a declarator,
5532 rather than as part of an expression. */
5533
5534static cp_expr
5535cp_parser_unqualified_id (cp_parser* parser,
5536 bool template_keyword_p,
5537 bool check_dependency_p,
5538 bool declarator_p,
5539 bool optional_p)
5540{
5541 cp_token *token;
5542
5543 /* Peek at the next token. */
5544 token = cp_lexer_peek_token (parser->lexer);
5545
5546 switch ((int) token->type)
5547 {
5548 case CPP_NAME:
5549 {
5550 tree id;
5551
5552 /* We don't know yet whether or not this will be a
5553 template-id. */
5554 cp_parser_parse_tentatively (parser);
5555 /* Try a template-id. */
5556 id = cp_parser_template_id (parser, template_keyword_p,
5557 check_dependency_p,
5558 none_type,
5559 declarator_p);
5560 /* If it worked, we're done. */
5561 if (cp_parser_parse_definitely (parser))
5562 return id;
5563 /* Otherwise, it's an ordinary identifier. */
5564 return cp_parser_identifier (parser);
5565 }
5566
5567 case CPP_TEMPLATE_ID:
5568 return cp_parser_template_id (parser, template_keyword_p,
5569 check_dependency_p,
5570 none_type,
5571 declarator_p);
5572
5573 case CPP_COMPL:
5574 {
5575 tree type_decl;
5576 tree qualifying_scope;
5577 tree object_scope;
5578 tree scope;
5579 bool done;
5580
5581 /* Consume the `~' token. */
5582 cp_lexer_consume_token (parser->lexer);
5583 /* Parse the class-name. The standard, as written, seems to
5584 say that:
5585
5586 template <typename T> struct S { ~S (); };
5587 template <typename T> S<T>::~S() {}
5588
5589 is invalid, since `~' must be followed by a class-name, but
5590 `S<T>' is dependent, and so not known to be a class.
5591 That's not right; we need to look in uninstantiated
5592 templates. A further complication arises from:
5593
5594 template <typename T> void f(T t) {
5595 t.T::~T();
5596 }
5597
5598 Here, it is not possible to look up `T' in the scope of `T'
5599 itself. We must look in both the current scope, and the
5600 scope of the containing complete expression.
5601
5602 Yet another issue is:
5603
5604 struct S {
5605 int S;
5606 ~S();
5607 };
5608
5609 S::~S() {}
5610
5611 The standard does not seem to say that the `S' in `~S'
5612 should refer to the type `S' and not the data member
5613 `S::S'. */
5614
5615 /* DR 244 says that we look up the name after the "~" in the
5616 same scope as we looked up the qualifying name. That idea
5617 isn't fully worked out; it's more complicated than that. */
5618 scope = parser->scope;
5619 object_scope = parser->object_scope;
5620 qualifying_scope = parser->qualifying_scope;
5621
5622 /* Check for invalid scopes. */
5623 if (scope == error_mark_node)
5624 {
5625 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5626 cp_lexer_consume_token (parser->lexer);
5627 return error_mark_node;
5628 }
5629 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5630 {
5631 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5632 error_at (token->location,
5633 "scope %qT before %<~%> is not a class-name",
5634 scope);
5635 cp_parser_simulate_error (parser);
5636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5637 cp_lexer_consume_token (parser->lexer);
5638 return error_mark_node;
5639 }
5640 gcc_assert (!scope || TYPE_P (scope));
5641
5642 /* If the name is of the form "X::~X" it's OK even if X is a
5643 typedef. */
5644 token = cp_lexer_peek_token (parser->lexer);
5645 if (scope
5646 && token->type == CPP_NAME
5647 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5648 != CPP_LESS)
5649 && (token->u.value == TYPE_IDENTIFIER (scope)
5650 || (CLASS_TYPE_P (scope)
5651 && constructor_name_p (token->u.value, scope))))
5652 {
5653 cp_lexer_consume_token (parser->lexer);
5654 return build_nt (BIT_NOT_EXPR, scope);
5655 }
5656
5657 /* ~auto means the destructor of whatever the object is. */
5658 if (cp_parser_is_keyword (token, RID_AUTO))
5659 {
5660 if (cxx_dialect < cxx14)
5661 pedwarn (input_location, 0,
5662 "%<~auto%> only available with "
5663 "-std=c++14 or -std=gnu++14");
5664 cp_lexer_consume_token (parser->lexer);
5665 return build_nt (BIT_NOT_EXPR, make_auto ());
5666 }
5667
5668 /* If there was an explicit qualification (S::~T), first look
5669 in the scope given by the qualification (i.e., S).
5670
5671 Note: in the calls to cp_parser_class_name below we pass
5672 typename_type so that lookup finds the injected-class-name
5673 rather than the constructor. */
5674 done = false;
5675 type_decl = NULL_TREE;
5676 if (scope)
5677 {
5678 cp_parser_parse_tentatively (parser);
5679 type_decl = cp_parser_class_name (parser,
5680 /*typename_keyword_p=*/false,
5681 /*template_keyword_p=*/false,
5682 typename_type,
5683 /*check_dependency=*/false,
5684 /*class_head_p=*/false,
5685 declarator_p);
5686 if (cp_parser_parse_definitely (parser))
5687 done = true;
5688 }
5689 /* In "N::S::~S", look in "N" as well. */
5690 if (!done && scope && qualifying_scope)
5691 {
5692 cp_parser_parse_tentatively (parser);
5693 parser->scope = qualifying_scope;
5694 parser->object_scope = NULL_TREE;
5695 parser->qualifying_scope = NULL_TREE;
5696 type_decl
5697 = cp_parser_class_name (parser,
5698 /*typename_keyword_p=*/false,
5699 /*template_keyword_p=*/false,
5700 typename_type,
5701 /*check_dependency=*/false,
5702 /*class_head_p=*/false,
5703 declarator_p);
5704 if (cp_parser_parse_definitely (parser))
5705 done = true;
5706 }
5707 /* In "p->S::~T", look in the scope given by "*p" as well. */
5708 else if (!done && object_scope)
5709 {
5710 cp_parser_parse_tentatively (parser);
5711 parser->scope = object_scope;
5712 parser->object_scope = NULL_TREE;
5713 parser->qualifying_scope = NULL_TREE;
5714 type_decl
5715 = cp_parser_class_name (parser,
5716 /*typename_keyword_p=*/false,
5717 /*template_keyword_p=*/false,
5718 typename_type,
5719 /*check_dependency=*/false,
5720 /*class_head_p=*/false,
5721 declarator_p);
5722 if (cp_parser_parse_definitely (parser))
5723 done = true;
5724 }
5725 /* Look in the surrounding context. */
5726 if (!done)
5727 {
5728 parser->scope = NULL_TREE;
5729 parser->object_scope = NULL_TREE;
5730 parser->qualifying_scope = NULL_TREE;
5731 if (processing_template_decl)
5732 cp_parser_parse_tentatively (parser);
5733 type_decl
5734 = cp_parser_class_name (parser,
5735 /*typename_keyword_p=*/false,
5736 /*template_keyword_p=*/false,
5737 typename_type,
5738 /*check_dependency=*/false,
5739 /*class_head_p=*/false,
5740 declarator_p);
5741 if (processing_template_decl
5742 && ! cp_parser_parse_definitely (parser))
5743 {
5744 /* We couldn't find a type with this name. If we're parsing
5745 tentatively, fail and try something else. */
5746 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5747 {
5748 cp_parser_simulate_error (parser);
5749 return error_mark_node;
5750 }
5751 /* Otherwise, accept it and check for a match at instantiation
5752 time. */
5753 type_decl = cp_parser_identifier (parser);
5754 if (type_decl != error_mark_node)
5755 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5756 return type_decl;
5757 }
5758 }
5759 /* If an error occurred, assume that the name of the
5760 destructor is the same as the name of the qualifying
5761 class. That allows us to keep parsing after running
5762 into ill-formed destructor names. */
5763 if (type_decl == error_mark_node && scope)
5764 return build_nt (BIT_NOT_EXPR, scope);
5765 else if (type_decl == error_mark_node)
5766 return error_mark_node;
5767
5768 /* Check that destructor name and scope match. */
5769 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5770 {
5771 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5772 error_at (token->location,
5773 "declaration of %<~%T%> as member of %qT",
5774 type_decl, scope);
5775 cp_parser_simulate_error (parser);
5776 return error_mark_node;
5777 }
5778
5779 /* [class.dtor]
5780
5781 A typedef-name that names a class shall not be used as the
5782 identifier in the declarator for a destructor declaration. */
5783 if (declarator_p
5784 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5785 && !DECL_SELF_REFERENCE_P (type_decl)
5786 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5787 error_at (token->location,
5788 "typedef-name %qD used as destructor declarator",
5789 type_decl);
5790
5791 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5792 }
5793
5794 case CPP_KEYWORD:
5795 if (token->keyword == RID_OPERATOR)
5796 {
5797 cp_expr id;
5798
5799 /* This could be a template-id, so we try that first. */
5800 cp_parser_parse_tentatively (parser);
5801 /* Try a template-id. */
5802 id = cp_parser_template_id (parser, template_keyword_p,
5803 /*check_dependency_p=*/true,
5804 none_type,
5805 declarator_p);
5806 /* If that worked, we're done. */
5807 if (cp_parser_parse_definitely (parser))
5808 return id;
5809 /* We still don't know whether we're looking at an
5810 operator-function-id or a conversion-function-id. */
5811 cp_parser_parse_tentatively (parser);
5812 /* Try an operator-function-id. */
5813 id = cp_parser_operator_function_id (parser);
5814 /* If that didn't work, try a conversion-function-id. */
5815 if (!cp_parser_parse_definitely (parser))
5816 id = cp_parser_conversion_function_id (parser);
5817 else if (UDLIT_OPER_P (id))
5818 {
5819 /* 17.6.3.3.5 */
5820 const char *name = UDLIT_OP_SUFFIX (id);
5821 if (name[0] != '_' && !in_system_header_at (input_location)
5822 && declarator_p)
5823 warning (OPT_Wliteral_suffix,
5824 "literal operator suffixes not preceded by %<_%>"
5825 " are reserved for future standardization");
5826 }
5827
5828 return id;
5829 }
5830 /* Fall through. */
5831
5832 default:
5833 if (optional_p)
5834 return NULL_TREE;
5835 cp_parser_error (parser, "expected unqualified-id");
5836 return error_mark_node;
5837 }
5838}
5839
5840/* Parse an (optional) nested-name-specifier.
5841
5842 nested-name-specifier: [C++98]
5843 class-or-namespace-name :: nested-name-specifier [opt]
5844 class-or-namespace-name :: template nested-name-specifier [opt]
5845
5846 nested-name-specifier: [C++0x]
5847 type-name ::
5848 namespace-name ::
5849 nested-name-specifier identifier ::
5850 nested-name-specifier template [opt] simple-template-id ::
5851
5852 PARSER->SCOPE should be set appropriately before this function is
5853 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5854 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5855 in name lookups.
5856
5857 Sets PARSER->SCOPE to the class (TYPE) or namespace
5858 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5859 it unchanged if there is no nested-name-specifier. Returns the new
5860 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5861
5862 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5863 part of a declaration and/or decl-specifier. */
5864
5865static tree
5866cp_parser_nested_name_specifier_opt (cp_parser *parser,
5867 bool typename_keyword_p,
5868 bool check_dependency_p,
5869 bool type_p,
5870 bool is_declaration,
5871 bool template_keyword_p /* = false */)
5872{
5873 bool success = false;
5874 cp_token_position start = 0;
5875 cp_token *token;
5876
5877 /* Remember where the nested-name-specifier starts. */
5878 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5879 {
5880 start = cp_lexer_token_position (parser->lexer, false);
5881 push_deferring_access_checks (dk_deferred);
5882 }
5883
5884 while (true)
5885 {
5886 tree new_scope;
5887 tree old_scope;
5888 tree saved_qualifying_scope;
5889
5890 /* Spot cases that cannot be the beginning of a
5891 nested-name-specifier. */
5892 token = cp_lexer_peek_token (parser->lexer);
5893
5894 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5895 the already parsed nested-name-specifier. */
5896 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5897 {
5898 /* Grab the nested-name-specifier and continue the loop. */
5899 cp_parser_pre_parsed_nested_name_specifier (parser);
5900 /* If we originally encountered this nested-name-specifier
5901 with IS_DECLARATION set to false, we will not have
5902 resolved TYPENAME_TYPEs, so we must do so here. */
5903 if (is_declaration
5904 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5905 {
5906 new_scope = resolve_typename_type (parser->scope,
5907 /*only_current_p=*/false);
5908 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5909 parser->scope = new_scope;
5910 }
5911 success = true;
5912 continue;
5913 }
5914
5915 /* Spot cases that cannot be the beginning of a
5916 nested-name-specifier. On the second and subsequent times
5917 through the loop, we look for the `template' keyword. */
5918 if (success && token->keyword == RID_TEMPLATE)
5919 ;
5920 /* A template-id can start a nested-name-specifier. */
5921 else if (token->type == CPP_TEMPLATE_ID)
5922 ;
5923 /* DR 743: decltype can be used in a nested-name-specifier. */
5924 else if (token_is_decltype (token))
5925 ;
5926 else
5927 {
5928 /* If the next token is not an identifier, then it is
5929 definitely not a type-name or namespace-name. */
5930 if (token->type != CPP_NAME)
5931 break;
5932 /* If the following token is neither a `<' (to begin a
5933 template-id), nor a `::', then we are not looking at a
5934 nested-name-specifier. */
5935 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5936
5937 if (token->type == CPP_COLON
5938 && parser->colon_corrects_to_scope_p
5939 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5940 {
5941 error_at (token->location,
5942 "found %<:%> in nested-name-specifier, expected %<::%>");
5943 token->type = CPP_SCOPE;
5944 }
5945
5946 if (token->type != CPP_SCOPE
5947 && !cp_parser_nth_token_starts_template_argument_list_p
5948 (parser, 2))
5949 break;
5950 }
5951
5952 /* The nested-name-specifier is optional, so we parse
5953 tentatively. */
5954 cp_parser_parse_tentatively (parser);
5955
5956 /* Look for the optional `template' keyword, if this isn't the
5957 first time through the loop. */
5958 if (success)
5959 template_keyword_p = cp_parser_optional_template_keyword (parser);
5960
5961 /* Save the old scope since the name lookup we are about to do
5962 might destroy it. */
5963 old_scope = parser->scope;
5964 saved_qualifying_scope = parser->qualifying_scope;
5965 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5966 look up names in "X<T>::I" in order to determine that "Y" is
5967 a template. So, if we have a typename at this point, we make
5968 an effort to look through it. */
5969 if (is_declaration
5970 && !typename_keyword_p
5971 && parser->scope
5972 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5973 parser->scope = resolve_typename_type (parser->scope,
5974 /*only_current_p=*/false);
5975 /* Parse the qualifying entity. */
5976 new_scope
5977 = cp_parser_qualifying_entity (parser,
5978 typename_keyword_p,
5979 template_keyword_p,
5980 check_dependency_p,
5981 type_p,
5982 is_declaration);
5983 /* Look for the `::' token. */
5984 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5985
5986 /* If we found what we wanted, we keep going; otherwise, we're
5987 done. */
5988 if (!cp_parser_parse_definitely (parser))
5989 {
5990 bool error_p = false;
5991
5992 /* Restore the OLD_SCOPE since it was valid before the
5993 failed attempt at finding the last
5994 class-or-namespace-name. */
5995 parser->scope = old_scope;
5996 parser->qualifying_scope = saved_qualifying_scope;
5997
5998 /* If the next token is a decltype, and the one after that is a
5999 `::', then the decltype has failed to resolve to a class or
6000 enumeration type. Give this error even when parsing
6001 tentatively since it can't possibly be valid--and we're going
6002 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6003 won't get another chance.*/
6004 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6005 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6006 == CPP_SCOPE))
6007 {
6008 token = cp_lexer_consume_token (parser->lexer);
6009 error_at (token->location, "decltype evaluates to %qT, "
6010 "which is not a class or enumeration type",
6011 token->u.tree_check_value->value);
6012 parser->scope = error_mark_node;
6013 error_p = true;
6014 /* As below. */
6015 success = true;
6016 cp_lexer_consume_token (parser->lexer);
6017 }
6018
6019 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6020 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6021 {
6022 /* If we have a non-type template-id followed by ::, it can't
6023 possibly be valid. */
6024 token = cp_lexer_peek_token (parser->lexer);
6025 tree tid = token->u.tree_check_value->value;
6026 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6027 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6028 {
6029 tree tmpl = NULL_TREE;
6030 if (is_overloaded_fn (tid))
6031 {
6032 tree fns = get_fns (tid);
6033 if (!OVL_CHAIN (fns))
6034 tmpl = OVL_CURRENT (fns);
6035 error_at (token->location, "function template-id %qD "
6036 "in nested-name-specifier", tid);
6037 }
6038 else
6039 {
6040 /* Variable template. */
6041 tmpl = TREE_OPERAND (tid, 0);
6042 gcc_assert (variable_template_p (tmpl));
6043 error_at (token->location, "variable template-id %qD "
6044 "in nested-name-specifier", tid);
6045 }
6046 if (tmpl)
6047 inform (DECL_SOURCE_LOCATION (tmpl),
6048 "%qD declared here", tmpl);
6049
6050 parser->scope = error_mark_node;
6051 error_p = true;
6052 /* As below. */
6053 success = true;
6054 cp_lexer_consume_token (parser->lexer);
6055 cp_lexer_consume_token (parser->lexer);
6056 }
6057 }
6058
6059 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6060 break;
6061 /* If the next token is an identifier, and the one after
6062 that is a `::', then any valid interpretation would have
6063 found a class-or-namespace-name. */
6064 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6065 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6066 == CPP_SCOPE)
6067 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6068 != CPP_COMPL))
6069 {
6070 token = cp_lexer_consume_token (parser->lexer);
6071 if (!error_p)
6072 {
6073 if (!token->error_reported)
6074 {
6075 tree decl;
6076 tree ambiguous_decls;
6077
6078 decl = cp_parser_lookup_name (parser, token->u.value,
6079 none_type,
6080 /*is_template=*/false,
6081 /*is_namespace=*/false,
6082 /*check_dependency=*/true,
6083 &ambiguous_decls,
6084 token->location);
6085 if (TREE_CODE (decl) == TEMPLATE_DECL)
6086 error_at (token->location,
6087 "%qD used without template parameters",
6088 decl);
6089 else if (ambiguous_decls)
6090 {
6091 // cp_parser_lookup_name has the same diagnostic,
6092 // thus make sure to emit it at most once.
6093 if (cp_parser_uncommitted_to_tentative_parse_p
6094 (parser))
6095 {
6096 error_at (token->location,
6097 "reference to %qD is ambiguous",
6098 token->u.value);
6099 print_candidates (ambiguous_decls);
6100 }
6101 decl = error_mark_node;
6102 }
6103 else
6104 {
6105 if (cxx_dialect != cxx98)
6106 cp_parser_name_lookup_error
6107 (parser, token->u.value, decl, NLE_NOT_CXX98,
6108 token->location);
6109 else
6110 cp_parser_name_lookup_error
6111 (parser, token->u.value, decl, NLE_CXX98,
6112 token->location);
6113 }
6114 }
6115 parser->scope = error_mark_node;
6116 error_p = true;
6117 /* Treat this as a successful nested-name-specifier
6118 due to:
6119
6120 [basic.lookup.qual]
6121
6122 If the name found is not a class-name (clause
6123 _class_) or namespace-name (_namespace.def_), the
6124 program is ill-formed. */
6125 success = true;
6126 }
6127 cp_lexer_consume_token (parser->lexer);
6128 }
6129 break;
6130 }
6131 /* We've found one valid nested-name-specifier. */
6132 success = true;
6133 /* Name lookup always gives us a DECL. */
6134 if (TREE_CODE (new_scope) == TYPE_DECL)
6135 new_scope = TREE_TYPE (new_scope);
6136 /* Uses of "template" must be followed by actual templates. */
6137 if (template_keyword_p
6138 && !(CLASS_TYPE_P (new_scope)
6139 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6140 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6141 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6142 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6143 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6144 == TEMPLATE_ID_EXPR)))
6145 permerror (input_location, TYPE_P (new_scope)
6146 ? G_("%qT is not a template")
6147 : G_("%qD is not a template"),
6148 new_scope);
6149 /* If it is a class scope, try to complete it; we are about to
6150 be looking up names inside the class. */
6151 if (TYPE_P (new_scope)
6152 /* Since checking types for dependency can be expensive,
6153 avoid doing it if the type is already complete. */
6154 && !COMPLETE_TYPE_P (new_scope)
6155 /* Do not try to complete dependent types. */
6156 && !dependent_type_p (new_scope))
6157 {
6158 new_scope = complete_type (new_scope);
6159 /* If it is a typedef to current class, use the current
6160 class instead, as the typedef won't have any names inside
6161 it yet. */
6162 if (!COMPLETE_TYPE_P (new_scope)
6163 && currently_open_class (new_scope))
6164 new_scope = TYPE_MAIN_VARIANT (new_scope);
6165 }
6166 /* Make sure we look in the right scope the next time through
6167 the loop. */
6168 parser->scope = new_scope;
6169 }
6170
6171 /* If parsing tentatively, replace the sequence of tokens that makes
6172 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6173 token. That way, should we re-parse the token stream, we will
6174 not have to repeat the effort required to do the parse, nor will
6175 we issue duplicate error messages. */
6176 if (success && start)
6177 {
6178 cp_token *token;
6179
6180 token = cp_lexer_token_at (parser->lexer, start);
6181 /* Reset the contents of the START token. */
6182 token->type = CPP_NESTED_NAME_SPECIFIER;
6183 /* Retrieve any deferred checks. Do not pop this access checks yet
6184 so the memory will not be reclaimed during token replacing below. */
6185 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6186 token->u.tree_check_value->value = parser->scope;
6187 token->u.tree_check_value->checks = get_deferred_access_checks ();
6188 token->u.tree_check_value->qualifying_scope =
6189 parser->qualifying_scope;
6190 token->keyword = RID_MAX;
6191
6192 /* Purge all subsequent tokens. */
6193 cp_lexer_purge_tokens_after (parser->lexer, start);
6194 }
6195
6196 if (start)
6197 pop_to_parent_deferring_access_checks ();
6198
6199 return success ? parser->scope : NULL_TREE;
6200}
6201
6202/* Parse a nested-name-specifier. See
6203 cp_parser_nested_name_specifier_opt for details. This function
6204 behaves identically, except that it will an issue an error if no
6205 nested-name-specifier is present. */
6206
6207static tree
6208cp_parser_nested_name_specifier (cp_parser *parser,
6209 bool typename_keyword_p,
6210 bool check_dependency_p,
6211 bool type_p,
6212 bool is_declaration)
6213{
6214 tree scope;
6215
6216 /* Look for the nested-name-specifier. */
6217 scope = cp_parser_nested_name_specifier_opt (parser,
6218 typename_keyword_p,
6219 check_dependency_p,
6220 type_p,
6221 is_declaration);
6222 /* If it was not present, issue an error message. */
6223 if (!scope)
6224 {
6225 cp_parser_error (parser, "expected nested-name-specifier");
6226 parser->scope = NULL_TREE;
6227 }
6228
6229 return scope;
6230}
6231
6232/* Parse the qualifying entity in a nested-name-specifier. For C++98,
6233 this is either a class-name or a namespace-name (which corresponds
6234 to the class-or-namespace-name production in the grammar). For
6235 C++0x, it can also be a type-name that refers to an enumeration
6236 type or a simple-template-id.
6237
6238 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6239 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6240 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6241 TYPE_P is TRUE iff the next name should be taken as a class-name,
6242 even the same name is declared to be another entity in the same
6243 scope.
6244
6245 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6246 specified by the class-or-namespace-name. If neither is found the
6247 ERROR_MARK_NODE is returned. */
6248
6249static tree
6250cp_parser_qualifying_entity (cp_parser *parser,
6251 bool typename_keyword_p,
6252 bool template_keyword_p,
6253 bool check_dependency_p,
6254 bool type_p,
6255 bool is_declaration)
6256{
6257 tree saved_scope;
6258 tree saved_qualifying_scope;
6259 tree saved_object_scope;
6260 tree scope;
6261 bool only_class_p;
6262 bool successful_parse_p;
6263
6264 /* DR 743: decltype can appear in a nested-name-specifier. */
6265 if (cp_lexer_next_token_is_decltype (parser->lexer))
6266 {
6267 scope = cp_parser_decltype (parser);
6268 if (TREE_CODE (scope) != ENUMERAL_TYPE
6269 && !MAYBE_CLASS_TYPE_P (scope))
6270 {
6271 cp_parser_simulate_error (parser);
6272 return error_mark_node;
6273 }
6274 if (TYPE_NAME (scope))
6275 scope = TYPE_NAME (scope);
6276 return scope;
6277 }
6278
6279 /* Before we try to parse the class-name, we must save away the
6280 current PARSER->SCOPE since cp_parser_class_name will destroy
6281 it. */
6282 saved_scope = parser->scope;
6283 saved_qualifying_scope = parser->qualifying_scope;
6284 saved_object_scope = parser->object_scope;
6285 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6286 there is no need to look for a namespace-name. */
6287 only_class_p = template_keyword_p
6288 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6289 if (!only_class_p)
6290 cp_parser_parse_tentatively (parser);
6291 scope = cp_parser_class_name (parser,
6292 typename_keyword_p,
6293 template_keyword_p,
6294 type_p ? class_type : none_type,
6295 check_dependency_p,
6296 /*class_head_p=*/false,
6297 is_declaration,
6298 /*enum_ok=*/cxx_dialect > cxx98);
6299 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6300 /* If that didn't work, try for a namespace-name. */
6301 if (!only_class_p && !successful_parse_p)
6302 {
6303 /* Restore the saved scope. */
6304 parser->scope = saved_scope;
6305 parser->qualifying_scope = saved_qualifying_scope;
6306 parser->object_scope = saved_object_scope;
6307 /* If we are not looking at an identifier followed by the scope
6308 resolution operator, then this is not part of a
6309 nested-name-specifier. (Note that this function is only used
6310 to parse the components of a nested-name-specifier.) */
6311 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6312 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6313 return error_mark_node;
6314 scope = cp_parser_namespace_name (parser);
6315 }
6316
6317 return scope;
6318}
6319
6320/* Return true if we are looking at a compound-literal, false otherwise. */
6321
6322static bool
6323cp_parser_compound_literal_p (cp_parser *parser)
6324{
6325 /* Consume the `('. */
6326 cp_lexer_consume_token (parser->lexer);
6327
6328 cp_lexer_save_tokens (parser->lexer);
6329
6330 /* Skip tokens until the next token is a closing parenthesis.
6331 If we find the closing `)', and the next token is a `{', then
6332 we are looking at a compound-literal. */
6333 bool compound_literal_p
6334 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6335 /*consume_paren=*/true)
6336 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6337
6338 /* Roll back the tokens we skipped. */
6339 cp_lexer_rollback_tokens (parser->lexer);
6340
6341 return compound_literal_p;
6342}
6343
6344/* Parse a postfix-expression.
6345
6346 postfix-expression:
6347 primary-expression
6348 postfix-expression [ expression ]
6349 postfix-expression ( expression-list [opt] )
6350 simple-type-specifier ( expression-list [opt] )
6351 typename :: [opt] nested-name-specifier identifier
6352 ( expression-list [opt] )
6353 typename :: [opt] nested-name-specifier template [opt] template-id
6354 ( expression-list [opt] )
6355 postfix-expression . template [opt] id-expression
6356 postfix-expression -> template [opt] id-expression
6357 postfix-expression . pseudo-destructor-name
6358 postfix-expression -> pseudo-destructor-name
6359 postfix-expression ++
6360 postfix-expression --
6361 dynamic_cast < type-id > ( expression )
6362 static_cast < type-id > ( expression )
6363 reinterpret_cast < type-id > ( expression )
6364 const_cast < type-id > ( expression )
6365 typeid ( expression )
6366 typeid ( type-id )
6367
6368 GNU Extension:
6369
6370 postfix-expression:
6371 ( type-id ) { initializer-list , [opt] }
6372
6373 This extension is a GNU version of the C99 compound-literal
6374 construct. (The C99 grammar uses `type-name' instead of `type-id',
6375 but they are essentially the same concept.)
6376
6377 If ADDRESS_P is true, the postfix expression is the operand of the
6378 `&' operator. CAST_P is true if this expression is the target of a
6379 cast.
6380
6381 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6382 class member access expressions [expr.ref].
6383
6384 Returns a representation of the expression. */
6385
6386static cp_expr
6387cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6388 bool member_access_only_p, bool decltype_p,
6389 cp_id_kind * pidk_return)
6390{
6391 cp_token *token;
6392 location_t loc;
6393 enum rid keyword;
6394 cp_id_kind idk = CP_ID_KIND_NONE;
6395 cp_expr postfix_expression = NULL_TREE;
6396 bool is_member_access = false;
6397 int saved_in_statement = -1;
6398
6399 /* Peek at the next token. */
6400 token = cp_lexer_peek_token (parser->lexer);
6401 loc = token->location;
6402 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6403
6404 /* Some of the productions are determined by keywords. */
6405 keyword = token->keyword;
6406 switch (keyword)
6407 {
6408 case RID_DYNCAST:
6409 case RID_STATCAST:
6410 case RID_REINTCAST:
6411 case RID_CONSTCAST:
6412 {
6413 tree type;
6414 cp_expr expression;
6415 const char *saved_message;
6416 bool saved_in_type_id_in_expr_p;
6417
6418 /* All of these can be handled in the same way from the point
6419 of view of parsing. Begin by consuming the token
6420 identifying the cast. */
6421 cp_lexer_consume_token (parser->lexer);
6422
6423 /* New types cannot be defined in the cast. */
6424 saved_message = parser->type_definition_forbidden_message;
6425 parser->type_definition_forbidden_message
6426 = G_("types may not be defined in casts");
6427
6428 /* Look for the opening `<'. */
6429 cp_parser_require (parser, CPP_LESS, RT_LESS);
6430 /* Parse the type to which we are casting. */
6431 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6432 parser->in_type_id_in_expr_p = true;
6433 type = cp_parser_type_id (parser);
6434 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6435 /* Look for the closing `>'. */
6436 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6437 /* Restore the old message. */
6438 parser->type_definition_forbidden_message = saved_message;
6439
6440 bool saved_greater_than_is_operator_p
6441 = parser->greater_than_is_operator_p;
6442 parser->greater_than_is_operator_p = true;
6443
6444 /* And the expression which is being cast. */
6445 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6446 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6447 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6448 RT_CLOSE_PAREN);
6449 location_t end_loc = close_paren ?
6450 close_paren->location : UNKNOWN_LOCATION;
6451
6452 parser->greater_than_is_operator_p
6453 = saved_greater_than_is_operator_p;
6454
6455 /* Only type conversions to integral or enumeration types
6456 can be used in constant-expressions. */
6457 if (!cast_valid_in_integral_constant_expression_p (type)
6458 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6459 {
6460 postfix_expression = error_mark_node;
6461 break;
6462 }
6463
6464 switch (keyword)
6465 {
6466 case RID_DYNCAST:
6467 postfix_expression
6468 = build_dynamic_cast (type, expression, tf_warning_or_error);
6469 break;
6470 case RID_STATCAST:
6471 postfix_expression
6472 = build_static_cast (type, expression, tf_warning_or_error);
6473 break;
6474 case RID_REINTCAST:
6475 postfix_expression
6476 = build_reinterpret_cast (type, expression,
6477 tf_warning_or_error);
6478 break;
6479 case RID_CONSTCAST:
6480 postfix_expression
6481 = build_const_cast (type, expression, tf_warning_or_error);
6482 break;
6483 default:
6484 gcc_unreachable ();
6485 }
6486
6487 /* Construct a location e.g. :
6488 reinterpret_cast <int *> (expr)
6489 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6490 ranging from the start of the "*_cast" token to the final closing
6491 paren, with the caret at the start. */
6492 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6493 postfix_expression.set_location (cp_cast_loc);
6494 }
6495 break;
6496
6497 case RID_TYPEID:
6498 {
6499 tree type;
6500 const char *saved_message;
6501 bool saved_in_type_id_in_expr_p;
6502
6503 /* Consume the `typeid' token. */
6504 cp_lexer_consume_token (parser->lexer);
6505 /* Look for the `(' token. */
6506 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6507 /* Types cannot be defined in a `typeid' expression. */
6508 saved_message = parser->type_definition_forbidden_message;
6509 parser->type_definition_forbidden_message
6510 = G_("types may not be defined in a %<typeid%> expression");
6511 /* We can't be sure yet whether we're looking at a type-id or an
6512 expression. */
6513 cp_parser_parse_tentatively (parser);
6514 /* Try a type-id first. */
6515 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6516 parser->in_type_id_in_expr_p = true;
6517 type = cp_parser_type_id (parser);
6518 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6519 /* Look for the `)' token. Otherwise, we can't be sure that
6520 we're not looking at an expression: consider `typeid (int
6521 (3))', for example. */
6522 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6523 /* If all went well, simply lookup the type-id. */
6524 if (cp_parser_parse_definitely (parser))
6525 postfix_expression = get_typeid (type, tf_warning_or_error);
6526 /* Otherwise, fall back to the expression variant. */
6527 else
6528 {
6529 tree expression;
6530
6531 /* Look for an expression. */
6532 expression = cp_parser_expression (parser, & idk);
6533 /* Compute its typeid. */
6534 postfix_expression = build_typeid (expression, tf_warning_or_error);
6535 /* Look for the `)' token. */
6536 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6537 }
6538 /* Restore the saved message. */
6539 parser->type_definition_forbidden_message = saved_message;
6540 /* `typeid' may not appear in an integral constant expression. */
6541 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6542 postfix_expression = error_mark_node;
6543 }
6544 break;
6545
6546 case RID_TYPENAME:
6547 {
6548 tree type;
6549 /* The syntax permitted here is the same permitted for an
6550 elaborated-type-specifier. */
6551 ++parser->prevent_constrained_type_specifiers;
6552 type = cp_parser_elaborated_type_specifier (parser,
6553 /*is_friend=*/false,
6554 /*is_declaration=*/false);
6555 --parser->prevent_constrained_type_specifiers;
6556 postfix_expression = cp_parser_functional_cast (parser, type);
6557 }
6558 break;
6559
6560 case RID_CILK_SPAWN:
6561 {
6562 location_t cilk_spawn_loc
6563 = cp_lexer_peek_token (parser->lexer)->location;
6564 cp_lexer_consume_token (parser->lexer);
6565 token = cp_lexer_peek_token (parser->lexer);
6566 if (token->type == CPP_SEMICOLON)
6567 {
6568 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6569 "an expression");
6570 postfix_expression = error_mark_node;
6571 break;
6572 }
6573 else if (!current_function_decl)
6574 {
6575 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6576 "inside a function");
6577 postfix_expression = error_mark_node;
6578 break;
6579 }
6580 else
6581 {
6582 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6583 saved_in_statement = parser->in_statement;
6584 parser->in_statement |= IN_CILK_SPAWN;
6585 }
6586 cfun->calls_cilk_spawn = 1;
6587 postfix_expression =
6588 cp_parser_postfix_expression (parser, false, false,
6589 false, false, &idk);
6590 if (!flag_cilkplus)
6591 {
6592 error_at (token->location, "-fcilkplus must be enabled to use"
6593 " %<_Cilk_spawn%>");
6594 cfun->calls_cilk_spawn = 0;
6595 }
6596 else if (saved_in_statement & IN_CILK_SPAWN)
6597 {
6598 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6599 "are not permitted");
6600 postfix_expression = error_mark_node;
6601 cfun->calls_cilk_spawn = 0;
6602 }
6603 else
6604 {
6605 location_t loc = postfix_expression.get_location ();
6606 postfix_expression = build_cilk_spawn (token->location,
6607 postfix_expression);
6608 /* Build a location of the form:
6609 _Cilk_spawn expr
6610 ~~~~~~~~~~~~^~~~
6611 with caret at the expr, ranging from the start of the
6612 _Cilk_spawn token to the end of the expression. */
6613 location_t combined_loc =
6614 make_location (loc, cilk_spawn_loc, get_finish (loc));
6615 postfix_expression.set_location (combined_loc);
6616 if (postfix_expression != error_mark_node)
6617 SET_EXPR_LOCATION (postfix_expression, input_location);
6618 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6619 }
6620 break;
6621 }
6622
6623 case RID_ADDRESSOF:
6624 case RID_BUILTIN_SHUFFLE:
6625 case RID_BUILTIN_LAUNDER:
6626 {
6627 vec<tree, va_gc> *vec;
6628 unsigned int i;
6629 tree p;
6630
6631 cp_lexer_consume_token (parser->lexer);
6632 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6633 /*cast_p=*/false, /*allow_expansion_p=*/true,
6634 /*non_constant_p=*/NULL);
6635 if (vec == NULL)
6636 {
6637 postfix_expression = error_mark_node;
6638 break;
6639 }
6640
6641 FOR_EACH_VEC_ELT (*vec, i, p)
6642 mark_exp_read (p);
6643
6644 switch (keyword)
6645 {
6646 case RID_ADDRESSOF:
6647 if (vec->length () == 1)
6648 postfix_expression
6649 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6650 else
6651 {
6652 error_at (loc, "wrong number of arguments to "
6653 "%<__builtin_addressof%>");
6654 postfix_expression = error_mark_node;
6655 }
6656 break;
6657
6658 case RID_BUILTIN_LAUNDER:
6659 if (vec->length () == 1)
6660 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6661 tf_warning_or_error);
6662 else
6663 {
6664 error_at (loc, "wrong number of arguments to "
6665 "%<__builtin_launder%>");
6666 postfix_expression = error_mark_node;
6667 }
6668 break;
6669
6670 case RID_BUILTIN_SHUFFLE:
6671 if (vec->length () == 2)
6672 postfix_expression
6673 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6674 (*vec)[1], tf_warning_or_error);
6675 else if (vec->length () == 3)
6676 postfix_expression
6677 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6678 (*vec)[2], tf_warning_or_error);
6679 else
6680 {
6681 error_at (loc, "wrong number of arguments to "
6682 "%<__builtin_shuffle%>");
6683 postfix_expression = error_mark_node;
6684 }
6685 break;
6686
6687 default:
6688 gcc_unreachable ();
6689 }
6690 break;
6691 }
6692
6693 default:
6694 {
6695 tree type;
6696
6697 /* If the next thing is a simple-type-specifier, we may be
6698 looking at a functional cast. We could also be looking at
6699 an id-expression. So, we try the functional cast, and if
6700 that doesn't work we fall back to the primary-expression. */
6701 cp_parser_parse_tentatively (parser);
6702 /* Look for the simple-type-specifier. */
6703 ++parser->prevent_constrained_type_specifiers;
6704 type = cp_parser_simple_type_specifier (parser,
6705 /*decl_specs=*/NULL,
6706 CP_PARSER_FLAGS_NONE);
6707 --parser->prevent_constrained_type_specifiers;
6708 /* Parse the cast itself. */
6709 if (!cp_parser_error_occurred (parser))
6710 postfix_expression
6711 = cp_parser_functional_cast (parser, type);
6712 /* If that worked, we're done. */
6713 if (cp_parser_parse_definitely (parser))
6714 break;
6715
6716 /* If the functional-cast didn't work out, try a
6717 compound-literal. */
6718 if (cp_parser_allow_gnu_extensions_p (parser)
6719 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6720 {
6721 cp_expr initializer = NULL_TREE;
6722
6723 cp_parser_parse_tentatively (parser);
6724
6725 /* Avoid calling cp_parser_type_id pointlessly, see comment
6726 in cp_parser_cast_expression about c++/29234. */
6727 if (!cp_parser_compound_literal_p (parser))
6728 cp_parser_simulate_error (parser);
6729 else
6730 {
6731 /* Parse the type. */
6732 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6733 parser->in_type_id_in_expr_p = true;
6734 type = cp_parser_type_id (parser);
6735 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6736 /* Look for the `)'. */
6737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6738 }
6739
6740 /* If things aren't going well, there's no need to
6741 keep going. */
6742 if (!cp_parser_error_occurred (parser))
6743 {
6744 bool non_constant_p;
6745 /* Parse the brace-enclosed initializer list. */
6746 initializer = cp_parser_braced_list (parser,
6747 &non_constant_p);
6748 }
6749 /* If that worked, we're definitely looking at a
6750 compound-literal expression. */
6751 if (cp_parser_parse_definitely (parser))
6752 {
6753 /* Warn the user that a compound literal is not
6754 allowed in standard C++. */
6755 pedwarn (input_location, OPT_Wpedantic,
6756 "ISO C++ forbids compound-literals");
6757 /* For simplicity, we disallow compound literals in
6758 constant-expressions. We could
6759 allow compound literals of integer type, whose
6760 initializer was a constant, in constant
6761 expressions. Permitting that usage, as a further
6762 extension, would not change the meaning of any
6763 currently accepted programs. (Of course, as
6764 compound literals are not part of ISO C++, the
6765 standard has nothing to say.) */
6766 if (cp_parser_non_integral_constant_expression (parser,
6767 NIC_NCC))
6768 {
6769 postfix_expression = error_mark_node;
6770 break;
6771 }
6772 /* Form the representation of the compound-literal. */
6773 postfix_expression
6774 = finish_compound_literal (type, initializer,
6775 tf_warning_or_error);
6776 postfix_expression.set_location (initializer.get_location ());
6777 break;
6778 }
6779 }
6780
6781 /* It must be a primary-expression. */
6782 postfix_expression
6783 = cp_parser_primary_expression (parser, address_p, cast_p,
6784 /*template_arg_p=*/false,
6785 decltype_p,
6786 &idk);
6787 }
6788 break;
6789 }
6790
6791 /* Note that we don't need to worry about calling build_cplus_new on a
6792 class-valued CALL_EXPR in decltype when it isn't the end of the
6793 postfix-expression; unary_complex_lvalue will take care of that for
6794 all these cases. */
6795
6796 /* Keep looping until the postfix-expression is complete. */
6797 while (true)
6798 {
6799 if (idk == CP_ID_KIND_UNQUALIFIED
6800 && identifier_p (postfix_expression)
6801 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6802 /* It is not a Koenig lookup function call. */
6803 postfix_expression
6804 = unqualified_name_lookup_error (postfix_expression);
6805
6806 /* Peek at the next token. */
6807 token = cp_lexer_peek_token (parser->lexer);
6808
6809 switch (token->type)
6810 {
6811 case CPP_OPEN_SQUARE:
6812 if (cp_next_tokens_can_be_std_attribute_p (parser))
6813 {
6814 cp_parser_error (parser,
6815 "two consecutive %<[%> shall "
6816 "only introduce an attribute");
6817 return error_mark_node;
6818 }
6819 postfix_expression
6820 = cp_parser_postfix_open_square_expression (parser,
6821 postfix_expression,
6822 false,
6823 decltype_p);
6824 postfix_expression.set_range (start_loc,
6825 postfix_expression.get_location ());
6826
6827 idk = CP_ID_KIND_NONE;
6828 is_member_access = false;
6829 break;
6830
6831 case CPP_OPEN_PAREN:
6832 /* postfix-expression ( expression-list [opt] ) */
6833 {
6834 bool koenig_p;
6835 bool is_builtin_constant_p;
6836 bool saved_integral_constant_expression_p = false;
6837 bool saved_non_integral_constant_expression_p = false;
6838 tsubst_flags_t complain = complain_flags (decltype_p);
6839 vec<tree, va_gc> *args;
6840 location_t close_paren_loc = UNKNOWN_LOCATION;
6841
6842 is_member_access = false;
6843
6844 is_builtin_constant_p
6845 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6846 if (is_builtin_constant_p)
6847 {
6848 /* The whole point of __builtin_constant_p is to allow
6849 non-constant expressions to appear as arguments. */
6850 saved_integral_constant_expression_p
6851 = parser->integral_constant_expression_p;
6852 saved_non_integral_constant_expression_p
6853 = parser->non_integral_constant_expression_p;
6854 parser->integral_constant_expression_p = false;
6855 }
6856 args = (cp_parser_parenthesized_expression_list
6857 (parser, non_attr,
6858 /*cast_p=*/false, /*allow_expansion_p=*/true,
6859 /*non_constant_p=*/NULL,
6860 /*close_paren_loc=*/&close_paren_loc));
6861 if (is_builtin_constant_p)
6862 {
6863 parser->integral_constant_expression_p
6864 = saved_integral_constant_expression_p;
6865 parser->non_integral_constant_expression_p
6866 = saved_non_integral_constant_expression_p;
6867 }
6868
6869 if (args == NULL)
6870 {
6871 postfix_expression = error_mark_node;
6872 break;
6873 }
6874
6875 /* Function calls are not permitted in
6876 constant-expressions. */
6877 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6878 && cp_parser_non_integral_constant_expression (parser,
6879 NIC_FUNC_CALL))
6880 {
6881 postfix_expression = error_mark_node;
6882 release_tree_vector (args);
6883 break;
6884 }
6885
6886 koenig_p = false;
6887 if (idk == CP_ID_KIND_UNQUALIFIED
6888 || idk == CP_ID_KIND_TEMPLATE_ID)
6889 {
6890 if (identifier_p (postfix_expression))
6891 {
6892 if (!args->is_empty ())
6893 {
6894 koenig_p = true;
6895 if (!any_type_dependent_arguments_p (args))
6896 postfix_expression
6897 = perform_koenig_lookup (postfix_expression, args,
6898 complain);
6899 }
6900 else
6901 postfix_expression
6902 = unqualified_fn_lookup_error (postfix_expression);
6903 }
6904 /* We do not perform argument-dependent lookup if
6905 normal lookup finds a non-function, in accordance
6906 with the expected resolution of DR 218. */
6907 else if (!args->is_empty ()
6908 && is_overloaded_fn (postfix_expression))
6909 {
6910 tree fn = get_first_fn (postfix_expression);
6911 fn = STRIP_TEMPLATE (fn);
6912
6913 /* Do not do argument dependent lookup if regular
6914 lookup finds a member function or a block-scope
6915 function declaration. [basic.lookup.argdep]/3 */
6916 if (!DECL_FUNCTION_MEMBER_P (fn)
6917 && !DECL_LOCAL_FUNCTION_P (fn))
6918 {
6919 koenig_p = true;
6920 if (!any_type_dependent_arguments_p (args))
6921 postfix_expression
6922 = perform_koenig_lookup (postfix_expression, args,
6923 complain);
6924 }
6925 }
6926 }
6927
6928 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6929 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6930 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6931 && vec_safe_length (args) == 3)
6932 {
6933 tree arg0 = (*args)[0];
6934 tree arg1 = (*args)[1];
6935 tree arg2 = (*args)[2];
6936 int literal_mask = ((!!integer_zerop (arg1) << 1)
6937 | (!!integer_zerop (arg2) << 2));
6938 if (TREE_CODE (arg2) == CONST_DECL)
6939 arg2 = DECL_INITIAL (arg2);
6940 warn_for_memset (input_location, arg0, arg2, literal_mask);
6941 }
6942
6943 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6944 {
6945 tree instance = TREE_OPERAND (postfix_expression, 0);
6946 tree fn = TREE_OPERAND (postfix_expression, 1);
6947
6948 if (processing_template_decl
6949 && (type_dependent_object_expression_p (instance)
6950 || (!BASELINK_P (fn)
6951 && TREE_CODE (fn) != FIELD_DECL)
6952 || type_dependent_expression_p (fn)
6953 || any_type_dependent_arguments_p (args)))
6954 {
6955 maybe_generic_this_capture (instance, fn);
6956 postfix_expression
6957 = build_nt_call_vec (postfix_expression, args);
6958 release_tree_vector (args);
6959 break;
6960 }
6961
6962 if (BASELINK_P (fn))
6963 {
6964 postfix_expression
6965 = (build_new_method_call
6966 (instance, fn, &args, NULL_TREE,
6967 (idk == CP_ID_KIND_QUALIFIED
6968 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6969 : LOOKUP_NORMAL),
6970 /*fn_p=*/NULL,
6971 complain));
6972 }
6973 else
6974 postfix_expression
6975 = finish_call_expr (postfix_expression, &args,
6976 /*disallow_virtual=*/false,
6977 /*koenig_p=*/false,
6978 complain);
6979 }
6980 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6981 || TREE_CODE (postfix_expression) == MEMBER_REF
6982 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6983 postfix_expression = (build_offset_ref_call_from_tree
6984 (postfix_expression, &args,
6985 complain));
6986 else if (idk == CP_ID_KIND_QUALIFIED)
6987 /* A call to a static class member, or a namespace-scope
6988 function. */
6989 postfix_expression
6990 = finish_call_expr (postfix_expression, &args,
6991 /*disallow_virtual=*/true,
6992 koenig_p,
6993 complain);
6994 else
6995 /* All other function calls. */
6996 postfix_expression
6997 = finish_call_expr (postfix_expression, &args,
6998 /*disallow_virtual=*/false,
6999 koenig_p,
7000 complain);
7001
7002 if (close_paren_loc != UNKNOWN_LOCATION)
7003 {
7004 location_t combined_loc = make_location (token->location,
7005 start_loc,
7006 close_paren_loc);
7007 postfix_expression.set_location (combined_loc);
7008 }
7009
7010 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7011 idk = CP_ID_KIND_NONE;
7012
7013 release_tree_vector (args);
7014 }
7015 break;
7016
7017 case CPP_DOT:
7018 case CPP_DEREF:
7019 /* postfix-expression . template [opt] id-expression
7020 postfix-expression . pseudo-destructor-name
7021 postfix-expression -> template [opt] id-expression
7022 postfix-expression -> pseudo-destructor-name */
7023
7024 /* Consume the `.' or `->' operator. */
7025 cp_lexer_consume_token (parser->lexer);
7026
7027 postfix_expression
7028 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7029 postfix_expression,
7030 false, &idk, loc);
7031
7032 is_member_access = true;
7033 break;
7034
7035 case CPP_PLUS_PLUS:
7036 /* postfix-expression ++ */
7037 /* Consume the `++' token. */
7038 cp_lexer_consume_token (parser->lexer);
7039 /* Generate a representation for the complete expression. */
7040 postfix_expression
7041 = finish_increment_expr (postfix_expression,
7042 POSTINCREMENT_EXPR);
7043 /* Increments may not appear in constant-expressions. */
7044 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7045 postfix_expression = error_mark_node;
7046 idk = CP_ID_KIND_NONE;
7047 is_member_access = false;
7048 break;
7049
7050 case CPP_MINUS_MINUS:
7051 /* postfix-expression -- */
7052 /* Consume the `--' token. */
7053 cp_lexer_consume_token (parser->lexer);
7054 /* Generate a representation for the complete expression. */
7055 postfix_expression
7056 = finish_increment_expr (postfix_expression,
7057 POSTDECREMENT_EXPR);
7058 /* Decrements may not appear in constant-expressions. */
7059 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7060 postfix_expression = error_mark_node;
7061 idk = CP_ID_KIND_NONE;
7062 is_member_access = false;
7063 break;
7064
7065 default:
7066 if (pidk_return != NULL)
7067 * pidk_return = idk;
7068 if (member_access_only_p)
7069 return is_member_access
7070 ? postfix_expression
7071 : cp_expr (error_mark_node);
7072 else
7073 return postfix_expression;
7074 }
7075 }
7076
7077 /* We should never get here. */
7078 gcc_unreachable ();
7079 return error_mark_node;
7080}
7081
7082/* This function parses Cilk Plus array notations. If a normal array expr. is
7083 parsed then the array index is passed back to the caller through *INIT_INDEX
7084 and the function returns a NULL_TREE. If array notation expr. is parsed,
7085 then *INIT_INDEX is ignored by the caller and the function returns
7086 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7087 error_mark_node. */
7088
7089static tree
7090cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7091 tree array_value)
7092{
7093 cp_token *token = NULL;
7094 tree length_index, stride = NULL_TREE, value_tree, array_type;
7095 if (!array_value || array_value == error_mark_node)
7096 {
7097 cp_parser_skip_to_end_of_statement (parser);
7098 return error_mark_node;
7099 }
7100
7101 array_type = TREE_TYPE (array_value);
7102
7103 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7104 parser->colon_corrects_to_scope_p = false;
7105 token = cp_lexer_peek_token (parser->lexer);
7106
7107 if (!token)
7108 {
7109 cp_parser_error (parser, "expected %<:%> or numeral");
7110 return error_mark_node;
7111 }
7112 else if (token->type == CPP_COLON)
7113 {
7114 /* Consume the ':'. */
7115 cp_lexer_consume_token (parser->lexer);
7116
7117 /* If we are here, then we have a case like this A[:]. */
7118 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7119 {
7120 cp_parser_error (parser, "expected %<]%>");
7121 cp_parser_skip_to_end_of_statement (parser);
7122 return error_mark_node;
7123 }
7124 *init_index = NULL_TREE;
7125 stride = NULL_TREE;
7126 length_index = NULL_TREE;
7127 }
7128 else
7129 {
7130 /* If we are here, then there are three valid possibilities:
7131 1. ARRAY [ EXP ]
7132 2. ARRAY [ EXP : EXP ]
7133 3. ARRAY [ EXP : EXP : EXP ] */
7134
7135 *init_index = cp_parser_expression (parser);
7136 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7137 {
7138 /* This indicates that we have a normal array expression. */
7139 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7140 return NULL_TREE;
7141 }
7142
7143 /* Consume the ':'. */
7144 cp_lexer_consume_token (parser->lexer);
7145 length_index = cp_parser_expression (parser);
7146 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7147 {
7148 cp_lexer_consume_token (parser->lexer);
7149 stride = cp_parser_expression (parser);
7150 }
7151 }
7152 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7153
7154 if (*init_index == error_mark_node || length_index == error_mark_node
7155 || stride == error_mark_node || array_type == error_mark_node)
7156 {
7157 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7158 cp_lexer_consume_token (parser->lexer);
7159 return error_mark_node;
7160 }
7161 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7162
7163 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7164 length_index, stride, array_type);
7165 return value_tree;
7166}
7167
7168/* A subroutine of cp_parser_postfix_expression that also gets hijacked
7169 by cp_parser_builtin_offsetof. We're looking for
7170
7171 postfix-expression [ expression ]
7172 postfix-expression [ braced-init-list ] (C++11)
7173
7174 FOR_OFFSETOF is set if we're being called in that context, which
7175 changes how we deal with integer constant expressions. */
7176
7177static tree
7178cp_parser_postfix_open_square_expression (cp_parser *parser,
7179 tree postfix_expression,
7180 bool for_offsetof,
7181 bool decltype_p)
7182{
7183 tree index = NULL_TREE;
7184 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7185 bool saved_greater_than_is_operator_p;
7186
7187 /* Consume the `[' token. */
7188 cp_lexer_consume_token (parser->lexer);
7189
7190 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7191 parser->greater_than_is_operator_p = true;
7192
7193 /* Parse the index expression. */
7194 /* ??? For offsetof, there is a question of what to allow here. If
7195 offsetof is not being used in an integral constant expression context,
7196 then we *could* get the right answer by computing the value at runtime.
7197 If we are in an integral constant expression context, then we might
7198 could accept any constant expression; hard to say without analysis.
7199 Rather than open the barn door too wide right away, allow only integer
7200 constant expressions here. */
7201 if (for_offsetof)
7202 index = cp_parser_constant_expression (parser);
7203 else
7204 {
7205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7206 {
7207 bool expr_nonconst_p;
7208 cp_lexer_set_source_position (parser->lexer);
7209 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7210 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7211 if (flag_cilkplus
7212 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7213 {
7214 error_at (cp_lexer_peek_token (parser->lexer)->location,
7215 "braced list index is not allowed with array "
7216 "notation");
7217 cp_parser_skip_to_end_of_statement (parser);
7218 return error_mark_node;
7219 }
7220 }
7221 else if (flag_cilkplus)
7222 {
7223 /* Here are have these two options:
7224 ARRAY[EXP : EXP] - Array notation expr with default
7225 stride of 1.
7226 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7227 stride. */
7228 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7229 postfix_expression);
7230 if (an_exp)
7231 return an_exp;
7232 }
7233 else
7234 index = cp_parser_expression (parser);
7235 }
7236
7237 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7238
7239 /* Look for the closing `]'. */
7240 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7241
7242 /* Build the ARRAY_REF. */
7243 postfix_expression = grok_array_decl (loc, postfix_expression,
7244 index, decltype_p);
7245
7246 /* When not doing offsetof, array references are not permitted in
7247 constant-expressions. */
7248 if (!for_offsetof
7249 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7250 postfix_expression = error_mark_node;
7251
7252 return postfix_expression;
7253}
7254
7255/* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7256 dereference of incomplete type, returns true if error_mark_node should
7257 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7258 and *DEPENDENT_P. */
7259
7260bool
7261cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7262 bool *dependent_p)
7263{
7264 /* In a template, be permissive by treating an object expression
7265 of incomplete type as dependent (after a pedwarn). */
7266 diagnostic_t kind = (processing_template_decl
7267 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7268
7269 switch (TREE_CODE (*postfix_expression))
7270 {
7271 case CAST_EXPR:
7272 case REINTERPRET_CAST_EXPR:
7273 case CONST_CAST_EXPR:
7274 case STATIC_CAST_EXPR:
7275 case DYNAMIC_CAST_EXPR:
7276 case IMPLICIT_CONV_EXPR:
7277 case VIEW_CONVERT_EXPR:
7278 case NON_LVALUE_EXPR:
7279 kind = DK_ERROR;
7280 break;
7281 case OVERLOAD:
7282 /* Don't emit any diagnostic for OVERLOADs. */
7283 kind = DK_IGNORED;
7284 break;
7285 default:
7286 /* Avoid clobbering e.g. DECLs. */
7287 if (!EXPR_P (*postfix_expression))
7288 kind = DK_ERROR;
7289 break;
7290 }
7291
7292 if (kind == DK_IGNORED)
7293 return false;
7294
7295 location_t exploc = location_of (*postfix_expression);
7296 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7297 if (!MAYBE_CLASS_TYPE_P (*scope))
7298 return true;
7299 if (kind == DK_ERROR)
7300 *scope = *postfix_expression = error_mark_node;
7301 else if (processing_template_decl)
7302 {
7303 *dependent_p = true;
7304 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7305 }
7306 return false;
7307}
7308
7309/* A subroutine of cp_parser_postfix_expression that also gets hijacked
7310 by cp_parser_builtin_offsetof. We're looking for
7311
7312 postfix-expression . template [opt] id-expression
7313 postfix-expression . pseudo-destructor-name
7314 postfix-expression -> template [opt] id-expression
7315 postfix-expression -> pseudo-destructor-name
7316
7317 FOR_OFFSETOF is set if we're being called in that context. That sorta
7318 limits what of the above we'll actually accept, but nevermind.
7319 TOKEN_TYPE is the "." or "->" token, which will already have been
7320 removed from the stream. */
7321
7322static tree
7323cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7324 enum cpp_ttype token_type,
7325 cp_expr postfix_expression,
7326 bool for_offsetof, cp_id_kind *idk,
7327 location_t location)
7328{
7329 tree name;
7330 bool dependent_p;
7331 bool pseudo_destructor_p;
7332 tree scope = NULL_TREE;
7333 location_t start_loc = postfix_expression.get_start ();
7334
7335 /* If this is a `->' operator, dereference the pointer. */
7336 if (token_type == CPP_DEREF)
7337 postfix_expression = build_x_arrow (location, postfix_expression,
7338 tf_warning_or_error);
7339 /* Check to see whether or not the expression is type-dependent and
7340 not the current instantiation. */
7341 dependent_p = type_dependent_object_expression_p (postfix_expression);
7342 /* The identifier following the `->' or `.' is not qualified. */
7343 parser->scope = NULL_TREE;
7344 parser->qualifying_scope = NULL_TREE;
7345 parser->object_scope = NULL_TREE;
7346 *idk = CP_ID_KIND_NONE;
7347
7348 /* Enter the scope corresponding to the type of the object
7349 given by the POSTFIX_EXPRESSION. */
7350 if (!dependent_p)
7351 {
7352 scope = TREE_TYPE (postfix_expression);
7353 /* According to the standard, no expression should ever have
7354 reference type. Unfortunately, we do not currently match
7355 the standard in this respect in that our internal representation
7356 of an expression may have reference type even when the standard
7357 says it does not. Therefore, we have to manually obtain the
7358 underlying type here. */
7359 scope = non_reference (scope);
7360 /* The type of the POSTFIX_EXPRESSION must be complete. */
7361 /* Unlike the object expression in other contexts, *this is not
7362 required to be of complete type for purposes of class member
7363 access (5.2.5) outside the member function body. */
7364 if (postfix_expression != current_class_ref
7365 && scope != error_mark_node
7366 && !(processing_template_decl
7367 && currently_open_class (scope)))
7368 {
7369 scope = complete_type (scope);
7370 if (!COMPLETE_TYPE_P (scope)
7371 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7372 &dependent_p))
7373 return error_mark_node;
7374 }
7375
7376 if (!dependent_p)
7377 {
7378 /* Let the name lookup machinery know that we are processing a
7379 class member access expression. */
7380 parser->context->object_type = scope;
7381 /* If something went wrong, we want to be able to discern that case,
7382 as opposed to the case where there was no SCOPE due to the type
7383 of expression being dependent. */
7384 if (!scope)
7385 scope = error_mark_node;
7386 /* If the SCOPE was erroneous, make the various semantic analysis
7387 functions exit quickly -- and without issuing additional error
7388 messages. */
7389 if (scope == error_mark_node)
7390 postfix_expression = error_mark_node;
7391 }
7392 }
7393
7394 if (dependent_p)
7395 /* Tell cp_parser_lookup_name that there was an object, even though it's
7396 type-dependent. */
7397 parser->context->object_type = unknown_type_node;
7398
7399 /* Assume this expression is not a pseudo-destructor access. */
7400 pseudo_destructor_p = false;
7401
7402 /* If the SCOPE is a scalar type, then, if this is a valid program,
7403 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7404 is type dependent, it can be pseudo-destructor-name or something else.
7405 Try to parse it as pseudo-destructor-name first. */
7406 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7407 {
7408 tree s;
7409 tree type;
7410
7411 cp_parser_parse_tentatively (parser);
7412 /* Parse the pseudo-destructor-name. */
7413 s = NULL_TREE;
7414 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7415 &s, &type);
7416 if (dependent_p
7417 && (cp_parser_error_occurred (parser)
7418 || !SCALAR_TYPE_P (type)))
7419 cp_parser_abort_tentative_parse (parser);
7420 else if (cp_parser_parse_definitely (parser))
7421 {
7422 pseudo_destructor_p = true;
7423 postfix_expression
7424 = finish_pseudo_destructor_expr (postfix_expression,
7425 s, type, location);
7426 }
7427 }
7428
7429 if (!pseudo_destructor_p)
7430 {
7431 /* If the SCOPE is not a scalar type, we are looking at an
7432 ordinary class member access expression, rather than a
7433 pseudo-destructor-name. */
7434 bool template_p;
7435 cp_token *token = cp_lexer_peek_token (parser->lexer);
7436 /* Parse the id-expression. */
7437 name = (cp_parser_id_expression
7438 (parser,
7439 cp_parser_optional_template_keyword (parser),
7440 /*check_dependency_p=*/true,
7441 &template_p,
7442 /*declarator_p=*/false,
7443 /*optional_p=*/false));
7444 /* In general, build a SCOPE_REF if the member name is qualified.
7445 However, if the name was not dependent and has already been
7446 resolved; there is no need to build the SCOPE_REF. For example;
7447
7448 struct X { void f(); };
7449 template <typename T> void f(T* t) { t->X::f(); }
7450
7451 Even though "t" is dependent, "X::f" is not and has been resolved
7452 to a BASELINK; there is no need to include scope information. */
7453
7454 /* But we do need to remember that there was an explicit scope for
7455 virtual function calls. */
7456 if (parser->scope)
7457 *idk = CP_ID_KIND_QUALIFIED;
7458
7459 /* If the name is a template-id that names a type, we will get a
7460 TYPE_DECL here. That is invalid code. */
7461 if (TREE_CODE (name) == TYPE_DECL)
7462 {
7463 error_at (token->location, "invalid use of %qD", name);
7464 postfix_expression = error_mark_node;
7465 }
7466 else
7467 {
7468 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7469 {
7470 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7471 {
7472 error_at (token->location, "%<%D::%D%> is not a class member",
7473 parser->scope, name);
7474 postfix_expression = error_mark_node;
7475 }
7476 else
7477 name = build_qualified_name (/*type=*/NULL_TREE,
7478 parser->scope,
7479 name,
7480 template_p);
7481 parser->scope = NULL_TREE;
7482 parser->qualifying_scope = NULL_TREE;
7483 parser->object_scope = NULL_TREE;
7484 }
7485 if (parser->scope && name && BASELINK_P (name))
7486 adjust_result_of_qualified_name_lookup
7487 (name, parser->scope, scope);
7488 postfix_expression
7489 = finish_class_member_access_expr (postfix_expression, name,
7490 template_p,
7491 tf_warning_or_error);
7492 /* Build a location e.g.:
7493 ptr->access_expr
7494 ~~~^~~~~~~~~~~~~
7495 where the caret is at the deref token, ranging from
7496 the start of postfix_expression to the end of the access expr. */
7497 location_t end_loc
7498 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7499 location_t combined_loc
7500 = make_location (input_location, start_loc, end_loc);
7501 protected_set_expr_location (postfix_expression, combined_loc);
7502 }
7503 }
7504
7505 /* We no longer need to look up names in the scope of the object on
7506 the left-hand side of the `.' or `->' operator. */
7507 parser->context->object_type = NULL_TREE;
7508
7509 /* Outside of offsetof, these operators may not appear in
7510 constant-expressions. */
7511 if (!for_offsetof
7512 && (cp_parser_non_integral_constant_expression
7513 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7514 postfix_expression = error_mark_node;
7515
7516 return postfix_expression;
7517}
7518
7519/* Parse a parenthesized expression-list.
7520
7521 expression-list:
7522 assignment-expression
7523 expression-list, assignment-expression
7524
7525 attribute-list:
7526 expression-list
7527 identifier
7528 identifier, expression-list
7529
7530 CAST_P is true if this expression is the target of a cast.
7531
7532 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7533 argument pack.
7534
7535 Returns a vector of trees. Each element is a representation of an
7536 assignment-expression. NULL is returned if the ( and or ) are
7537 missing. An empty, but allocated, vector is returned on no
7538 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7539 if we are parsing an attribute list for an attribute that wants a
7540 plain identifier argument, normal_attr for an attribute that wants
7541 an expression, or non_attr if we aren't parsing an attribute list. If
7542 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7543 not all of the expressions in the list were constant.
7544 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7545 will be written to with the location of the closing parenthesis. If
7546 an error occurs, it may or may not be written to. */
7547
7548static vec<tree, va_gc> *
7549cp_parser_parenthesized_expression_list (cp_parser* parser,
7550 int is_attribute_list,
7551 bool cast_p,
7552 bool allow_expansion_p,
7553 bool *non_constant_p,
7554 location_t *close_paren_loc)
7555{
7556 vec<tree, va_gc> *expression_list;
7557 bool fold_expr_p = is_attribute_list != non_attr;
7558 tree identifier = NULL_TREE;
7559 bool saved_greater_than_is_operator_p;
7560
7561 /* Assume all the expressions will be constant. */
7562 if (non_constant_p)
7563 *non_constant_p = false;
7564
7565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7566 return NULL;
7567
7568 expression_list = make_tree_vector ();
7569
7570 /* Within a parenthesized expression, a `>' token is always
7571 the greater-than operator. */
7572 saved_greater_than_is_operator_p
7573 = parser->greater_than_is_operator_p;
7574 parser->greater_than_is_operator_p = true;
7575
7576 /* Consume expressions until there are no more. */
7577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7578 while (true)
7579 {
7580 tree expr;
7581
7582 /* At the beginning of attribute lists, check to see if the
7583 next token is an identifier. */
7584 if (is_attribute_list == id_attr
7585 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7586 {
7587 cp_token *token;
7588
7589 /* Consume the identifier. */
7590 token = cp_lexer_consume_token (parser->lexer);
7591 /* Save the identifier. */
7592 identifier = token->u.value;
7593 }
7594 else
7595 {
7596 bool expr_non_constant_p;
7597
7598 /* Parse the next assignment-expression. */
7599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7600 {
7601 /* A braced-init-list. */
7602 cp_lexer_set_source_position (parser->lexer);
7603 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7604 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7605 if (non_constant_p && expr_non_constant_p)
7606 *non_constant_p = true;
7607 }
7608 else if (non_constant_p)
7609 {
7610 expr = (cp_parser_constant_expression
7611 (parser, /*allow_non_constant_p=*/true,
7612 &expr_non_constant_p));
7613 if (expr_non_constant_p)
7614 *non_constant_p = true;
7615 }
7616 else
7617 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7618 cast_p);
7619
7620 if (fold_expr_p)
7621 expr = instantiate_non_dependent_expr (expr);
7622
7623 /* If we have an ellipsis, then this is an expression
7624 expansion. */
7625 if (allow_expansion_p
7626 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7627 {
7628 /* Consume the `...'. */
7629 cp_lexer_consume_token (parser->lexer);
7630
7631 /* Build the argument pack. */
7632 expr = make_pack_expansion (expr);
7633 }
7634
7635 /* Add it to the list. We add error_mark_node
7636 expressions to the list, so that we can still tell if
7637 the correct form for a parenthesized expression-list
7638 is found. That gives better errors. */
7639 vec_safe_push (expression_list, expr);
7640
7641 if (expr == error_mark_node)
7642 goto skip_comma;
7643 }
7644
7645 /* After the first item, attribute lists look the same as
7646 expression lists. */
7647 is_attribute_list = non_attr;
7648
7649 get_comma:;
7650 /* If the next token isn't a `,', then we are done. */
7651 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7652 break;
7653
7654 /* Otherwise, consume the `,' and keep going. */
7655 cp_lexer_consume_token (parser->lexer);
7656 }
7657
7658 if (close_paren_loc)
7659 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7660
7661 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7662 {
7663 int ending;
7664
7665 skip_comma:;
7666 /* We try and resync to an unnested comma, as that will give the
7667 user better diagnostics. */
7668 ending = cp_parser_skip_to_closing_parenthesis (parser,
7669 /*recovering=*/true,
7670 /*or_comma=*/true,
7671 /*consume_paren=*/true);
7672 if (ending < 0)
7673 goto get_comma;
7674 if (!ending)
7675 {
7676 parser->greater_than_is_operator_p
7677 = saved_greater_than_is_operator_p;
7678 return NULL;
7679 }
7680 }
7681
7682 parser->greater_than_is_operator_p
7683 = saved_greater_than_is_operator_p;
7684
7685 if (identifier)
7686 vec_safe_insert (expression_list, 0, identifier);
7687
7688 return expression_list;
7689}
7690
7691/* Parse a pseudo-destructor-name.
7692
7693 pseudo-destructor-name:
7694 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7695 :: [opt] nested-name-specifier template template-id :: ~ type-name
7696 :: [opt] nested-name-specifier [opt] ~ type-name
7697
7698 If either of the first two productions is used, sets *SCOPE to the
7699 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7700 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7701 or ERROR_MARK_NODE if the parse fails. */
7702
7703static void
7704cp_parser_pseudo_destructor_name (cp_parser* parser,
7705 tree object,
7706 tree* scope,
7707 tree* type)
7708{
7709 bool nested_name_specifier_p;
7710
7711 /* Handle ~auto. */
7712 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7713 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7714 && !type_dependent_expression_p (object))
7715 {
7716 if (cxx_dialect < cxx14)
7717 pedwarn (input_location, 0,
7718 "%<~auto%> only available with "
7719 "-std=c++14 or -std=gnu++14");
7720 cp_lexer_consume_token (parser->lexer);
7721 cp_lexer_consume_token (parser->lexer);
7722 *scope = NULL_TREE;
7723 *type = TREE_TYPE (object);
7724 return;
7725 }
7726
7727 /* Assume that things will not work out. */
7728 *type = error_mark_node;
7729
7730 /* Look for the optional `::' operator. */
7731 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7732 /* Look for the optional nested-name-specifier. */
7733 nested_name_specifier_p
7734 = (cp_parser_nested_name_specifier_opt (parser,
7735 /*typename_keyword_p=*/false,
7736 /*check_dependency_p=*/true,
7737 /*type_p=*/false,
7738 /*is_declaration=*/false)
7739 != NULL_TREE);
7740 /* Now, if we saw a nested-name-specifier, we might be doing the
7741 second production. */
7742 if (nested_name_specifier_p
7743 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7744 {
7745 /* Consume the `template' keyword. */
7746 cp_lexer_consume_token (parser->lexer);
7747 /* Parse the template-id. */
7748 cp_parser_template_id (parser,
7749 /*template_keyword_p=*/true,
7750 /*check_dependency_p=*/false,
7751 class_type,
7752 /*is_declaration=*/true);
7753 /* Look for the `::' token. */
7754 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7755 }
7756 /* If the next token is not a `~', then there might be some
7757 additional qualification. */
7758 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7759 {
7760 /* At this point, we're looking for "type-name :: ~". The type-name
7761 must not be a class-name, since this is a pseudo-destructor. So,
7762 it must be either an enum-name, or a typedef-name -- both of which
7763 are just identifiers. So, we peek ahead to check that the "::"
7764 and "~" tokens are present; if they are not, then we can avoid
7765 calling type_name. */
7766 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7767 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7768 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7769 {
7770 cp_parser_error (parser, "non-scalar type");
7771 return;
7772 }
7773
7774 /* Look for the type-name. */
7775 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7776 if (*scope == error_mark_node)
7777 return;
7778
7779 /* Look for the `::' token. */
7780 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7781 }
7782 else
7783 *scope = NULL_TREE;
7784
7785 /* Look for the `~'. */
7786 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7787
7788 /* Once we see the ~, this has to be a pseudo-destructor. */
7789 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7790 cp_parser_commit_to_topmost_tentative_parse (parser);
7791
7792 /* Look for the type-name again. We are not responsible for
7793 checking that it matches the first type-name. */
7794 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7795}
7796
7797/* Parse a unary-expression.
7798
7799 unary-expression:
7800 postfix-expression
7801 ++ cast-expression
7802 -- cast-expression
7803 unary-operator cast-expression
7804 sizeof unary-expression
7805 sizeof ( type-id )
7806 alignof ( type-id ) [C++0x]
7807 new-expression
7808 delete-expression
7809
7810 GNU Extensions:
7811
7812 unary-expression:
7813 __extension__ cast-expression
7814 __alignof__ unary-expression
7815 __alignof__ ( type-id )
7816 alignof unary-expression [C++0x]
7817 __real__ cast-expression
7818 __imag__ cast-expression
7819 && identifier
7820 sizeof ( type-id ) { initializer-list , [opt] }
7821 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7822 __alignof__ ( type-id ) { initializer-list , [opt] }
7823
7824 ADDRESS_P is true iff the unary-expression is appearing as the
7825 operand of the `&' operator. CAST_P is true if this expression is
7826 the target of a cast.
7827
7828 Returns a representation of the expression. */
7829
7830static cp_expr
7831cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7832 bool address_p, bool cast_p, bool decltype_p)
7833{
7834 cp_token *token;
7835 enum tree_code unary_operator;
7836
7837 /* Peek at the next token. */
7838 token = cp_lexer_peek_token (parser->lexer);
7839 /* Some keywords give away the kind of expression. */
7840 if (token->type == CPP_KEYWORD)
7841 {
7842 enum rid keyword = token->keyword;
7843
7844 switch (keyword)
7845 {
7846 case RID_ALIGNOF:
7847 case RID_SIZEOF:
7848 {
7849 tree operand, ret;
7850 enum tree_code op;
7851 location_t first_loc;
7852
7853 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7854 /* Consume the token. */
7855 cp_lexer_consume_token (parser->lexer);
7856 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7857 /* Parse the operand. */
7858 operand = cp_parser_sizeof_operand (parser, keyword);
7859
7860 if (TYPE_P (operand))
7861 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7862 else
7863 {
7864 /* ISO C++ defines alignof only with types, not with
7865 expressions. So pedwarn if alignof is used with a non-
7866 type expression. However, __alignof__ is ok. */
7867 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7868 pedwarn (token->location, OPT_Wpedantic,
7869 "ISO C++ does not allow %<alignof%> "
7870 "with a non-type");
7871
7872 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7873 }
7874 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7875 SIZEOF_EXPR with the original operand. */
7876 if (op == SIZEOF_EXPR && ret != error_mark_node)
7877 {
7878 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7879 {
7880 if (!processing_template_decl && TYPE_P (operand))
7881 {
7882 ret = build_min (SIZEOF_EXPR, size_type_node,
7883 build1 (NOP_EXPR, operand,
7884 error_mark_node));
7885 SIZEOF_EXPR_TYPE_P (ret) = 1;
7886 }
7887 else
7888 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7889 TREE_SIDE_EFFECTS (ret) = 0;
7890 TREE_READONLY (ret) = 1;
7891 }
7892 SET_EXPR_LOCATION (ret, first_loc);
7893 }
7894 return ret;
7895 }
7896
7897 case RID_NEW:
7898 return cp_parser_new_expression (parser);
7899
7900 case RID_DELETE:
7901 return cp_parser_delete_expression (parser);
7902
7903 case RID_EXTENSION:
7904 {
7905 /* The saved value of the PEDANTIC flag. */
7906 int saved_pedantic;
7907 tree expr;
7908
7909 /* Save away the PEDANTIC flag. */
7910 cp_parser_extension_opt (parser, &saved_pedantic);
7911 /* Parse the cast-expression. */
7912 expr = cp_parser_simple_cast_expression (parser);
7913 /* Restore the PEDANTIC flag. */
7914 pedantic = saved_pedantic;
7915
7916 return expr;
7917 }
7918
7919 case RID_REALPART:
7920 case RID_IMAGPART:
7921 {
7922 tree expression;
7923
7924 /* Consume the `__real__' or `__imag__' token. */
7925 cp_lexer_consume_token (parser->lexer);
7926 /* Parse the cast-expression. */
7927 expression = cp_parser_simple_cast_expression (parser);
7928 /* Create the complete representation. */
7929 return build_x_unary_op (token->location,
7930 (keyword == RID_REALPART
7931 ? REALPART_EXPR : IMAGPART_EXPR),
7932 expression,
7933 tf_warning_or_error);
7934 }
7935 break;
7936
7937 case RID_TRANSACTION_ATOMIC:
7938 case RID_TRANSACTION_RELAXED:
7939 return cp_parser_transaction_expression (parser, keyword);
7940
7941 case RID_NOEXCEPT:
7942 {
7943 tree expr;
7944 const char *saved_message;
7945 bool saved_integral_constant_expression_p;
7946 bool saved_non_integral_constant_expression_p;
7947 bool saved_greater_than_is_operator_p;
7948
7949 cp_lexer_consume_token (parser->lexer);
7950 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7951
7952 saved_message = parser->type_definition_forbidden_message;
7953 parser->type_definition_forbidden_message
7954 = G_("types may not be defined in %<noexcept%> expressions");
7955
7956 saved_integral_constant_expression_p
7957 = parser->integral_constant_expression_p;
7958 saved_non_integral_constant_expression_p
7959 = parser->non_integral_constant_expression_p;
7960 parser->integral_constant_expression_p = false;
7961
7962 saved_greater_than_is_operator_p
7963 = parser->greater_than_is_operator_p;
7964 parser->greater_than_is_operator_p = true;
7965
7966 ++cp_unevaluated_operand;
7967 ++c_inhibit_evaluation_warnings;
7968 ++cp_noexcept_operand;
7969 expr = cp_parser_expression (parser);
7970 --cp_noexcept_operand;
7971 --c_inhibit_evaluation_warnings;
7972 --cp_unevaluated_operand;
7973
7974 parser->greater_than_is_operator_p
7975 = saved_greater_than_is_operator_p;
7976
7977 parser->integral_constant_expression_p
7978 = saved_integral_constant_expression_p;
7979 parser->non_integral_constant_expression_p
7980 = saved_non_integral_constant_expression_p;
7981
7982 parser->type_definition_forbidden_message = saved_message;
7983
7984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7985 return finish_noexcept_expr (expr, tf_warning_or_error);
7986 }
7987
7988 default:
7989 break;
7990 }
7991 }
7992
7993 /* Look for the `:: new' and `:: delete', which also signal the
7994 beginning of a new-expression, or delete-expression,
7995 respectively. If the next token is `::', then it might be one of
7996 these. */
7997 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7998 {
7999 enum rid keyword;
8000
8001 /* See if the token after the `::' is one of the keywords in
8002 which we're interested. */
8003 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8004 /* If it's `new', we have a new-expression. */
8005 if (keyword == RID_NEW)
8006 return cp_parser_new_expression (parser);
8007 /* Similarly, for `delete'. */
8008 else if (keyword == RID_DELETE)
8009 return cp_parser_delete_expression (parser);
8010 }
8011
8012 /* Look for a unary operator. */
8013 unary_operator = cp_parser_unary_operator (token);
8014 /* The `++' and `--' operators can be handled similarly, even though
8015 they are not technically unary-operators in the grammar. */
8016 if (unary_operator == ERROR_MARK)
8017 {
8018 if (token->type == CPP_PLUS_PLUS)
8019 unary_operator = PREINCREMENT_EXPR;
8020 else if (token->type == CPP_MINUS_MINUS)
8021 unary_operator = PREDECREMENT_EXPR;
8022 /* Handle the GNU address-of-label extension. */
8023 else if (cp_parser_allow_gnu_extensions_p (parser)
8024 && token->type == CPP_AND_AND)
8025 {
8026 tree identifier;
8027 tree expression;
8028 location_t start_loc = token->location;
8029
8030 /* Consume the '&&' token. */
8031 cp_lexer_consume_token (parser->lexer);
8032 /* Look for the identifier. */
8033 location_t finish_loc
8034 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8035 identifier = cp_parser_identifier (parser);
8036 /* Construct a location of the form:
8037 &&label
8038 ^~~~~~~
8039 with caret==start at the "&&", finish at the end of the label. */
8040 location_t combined_loc
8041 = make_location (start_loc, start_loc, finish_loc);
8042 /* Create an expression representing the address. */
8043 expression = finish_label_address_expr (identifier, combined_loc);
8044 if (cp_parser_non_integral_constant_expression (parser,
8045 NIC_ADDR_LABEL))
8046 expression = error_mark_node;
8047 return expression;
8048 }
8049 }
8050 if (unary_operator != ERROR_MARK)
8051 {
8052 cp_expr cast_expression;
8053 cp_expr expression = error_mark_node;
8054 non_integral_constant non_constant_p = NIC_NONE;
8055 location_t loc = token->location;
8056 tsubst_flags_t complain = complain_flags (decltype_p);
8057
8058 /* Consume the operator token. */
8059 token = cp_lexer_consume_token (parser->lexer);
8060 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8061
8062 /* Parse the cast-expression. */
8063 cast_expression
8064 = cp_parser_cast_expression (parser,
8065 unary_operator == ADDR_EXPR,
8066 /*cast_p=*/false,
8067 /*decltype*/false,
8068 pidk);
8069
8070 /* Make a location:
8071 OP_TOKEN CAST_EXPRESSION
8072 ^~~~~~~~~~~~~~~~~~~~~~~~~
8073 with start==caret at the operator token, and
8074 extending to the end of the cast_expression. */
8075 loc = make_location (loc, loc, cast_expression.get_finish ());
8076
8077 /* Now, build an appropriate representation. */
8078 switch (unary_operator)
8079 {
8080 case INDIRECT_REF:
8081 non_constant_p = NIC_STAR;
8082 expression = build_x_indirect_ref (loc, cast_expression,
8083 RO_UNARY_STAR,
8084 complain);
8085 /* TODO: build_x_indirect_ref does not always honor the
8086 location, so ensure it is set. */
8087 expression.set_location (loc);
8088 break;
8089
8090 case ADDR_EXPR:
8091 non_constant_p = NIC_ADDR;
8092 /* Fall through. */
8093 case BIT_NOT_EXPR:
8094 expression = build_x_unary_op (loc, unary_operator,
8095 cast_expression,
8096 complain);
8097 /* TODO: build_x_unary_op does not always honor the location,
8098 so ensure it is set. */
8099 expression.set_location (loc);
8100 break;
8101
8102 case PREINCREMENT_EXPR:
8103 case PREDECREMENT_EXPR:
8104 non_constant_p = unary_operator == PREINCREMENT_EXPR
8105 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8106 /* Fall through. */
8107 case NEGATE_EXPR:
8108 /* Immediately fold negation of a constant, unless the constant is 0
8109 (since -0 == 0) or it would overflow. */
8110 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8111 && CONSTANT_CLASS_P (cast_expression)
8112 && !integer_zerop (cast_expression)
8113 && !TREE_OVERFLOW (cast_expression))
8114 {
8115 tree folded = fold_build1 (unary_operator,
8116 TREE_TYPE (cast_expression),
8117 cast_expression);
8118 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8119 {
8120 expression = cp_expr (folded, loc);
8121 break;
8122 }
8123 }
8124 /* Fall through. */
8125 case UNARY_PLUS_EXPR:
8126 case TRUTH_NOT_EXPR:
8127 expression = finish_unary_op_expr (loc, unary_operator,
8128 cast_expression, complain);
8129 break;
8130
8131 default:
8132 gcc_unreachable ();
8133 }
8134
8135 if (non_constant_p != NIC_NONE
8136 && cp_parser_non_integral_constant_expression (parser,
8137 non_constant_p))
8138 expression = error_mark_node;
8139
8140 return expression;
8141 }
8142
8143 return cp_parser_postfix_expression (parser, address_p, cast_p,
8144 /*member_access_only_p=*/false,
8145 decltype_p,
8146 pidk);
8147}
8148
8149/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8150 unary-operator, the corresponding tree code is returned. */
8151
8152static enum tree_code
8153cp_parser_unary_operator (cp_token* token)
8154{
8155 switch (token->type)
8156 {
8157 case CPP_MULT:
8158 return INDIRECT_REF;
8159
8160 case CPP_AND:
8161 return ADDR_EXPR;
8162
8163 case CPP_PLUS:
8164 return UNARY_PLUS_EXPR;
8165
8166 case CPP_MINUS:
8167 return NEGATE_EXPR;
8168
8169 case CPP_NOT:
8170 return TRUTH_NOT_EXPR;
8171
8172 case CPP_COMPL:
8173 return BIT_NOT_EXPR;
8174
8175 default:
8176 return ERROR_MARK;
8177 }
8178}
8179
8180/* Parse a new-expression.
8181
8182 new-expression:
8183 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8184 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8185
8186 Returns a representation of the expression. */
8187
8188static tree
8189cp_parser_new_expression (cp_parser* parser)
8190{
8191 bool global_scope_p;
8192 vec<tree, va_gc> *placement;
8193 tree type;
8194 vec<tree, va_gc> *initializer;
8195 tree nelts = NULL_TREE;
8196 tree ret;
8197
8198 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8199
8200 /* Look for the optional `::' operator. */
8201 global_scope_p
8202 = (cp_parser_global_scope_opt (parser,
8203 /*current_scope_valid_p=*/false)
8204 != NULL_TREE);
8205 /* Look for the `new' operator. */
8206 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8207 /* There's no easy way to tell a new-placement from the
8208 `( type-id )' construct. */
8209 cp_parser_parse_tentatively (parser);
8210 /* Look for a new-placement. */
8211 placement = cp_parser_new_placement (parser);
8212 /* If that didn't work out, there's no new-placement. */
8213 if (!cp_parser_parse_definitely (parser))
8214 {
8215 if (placement != NULL)
8216 release_tree_vector (placement);
8217 placement = NULL;
8218 }
8219
8220 /* If the next token is a `(', then we have a parenthesized
8221 type-id. */
8222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8223 {
8224 cp_token *token;
8225 const char *saved_message = parser->type_definition_forbidden_message;
8226
8227 /* Consume the `('. */
8228 cp_lexer_consume_token (parser->lexer);
8229
8230 /* Parse the type-id. */
8231 parser->type_definition_forbidden_message
8232 = G_("types may not be defined in a new-expression");
8233 {
8234 type_id_in_expr_sentinel s (parser);
8235 type = cp_parser_type_id (parser);
8236 }
8237 parser->type_definition_forbidden_message = saved_message;
8238
8239 /* Look for the closing `)'. */
8240 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8241 token = cp_lexer_peek_token (parser->lexer);
8242 /* There should not be a direct-new-declarator in this production,
8243 but GCC used to allowed this, so we check and emit a sensible error
8244 message for this case. */
8245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8246 {
8247 error_at (token->location,
8248 "array bound forbidden after parenthesized type-id");
8249 inform (token->location,
8250 "try removing the parentheses around the type-id");
8251 cp_parser_direct_new_declarator (parser);
8252 }
8253 }
8254 /* Otherwise, there must be a new-type-id. */
8255 else
8256 type = cp_parser_new_type_id (parser, &nelts);
8257
8258 /* If the next token is a `(' or '{', then we have a new-initializer. */
8259 cp_token *token = cp_lexer_peek_token (parser->lexer);
8260 if (token->type == CPP_OPEN_PAREN
8261 || token->type == CPP_OPEN_BRACE)
8262 initializer = cp_parser_new_initializer (parser);
8263 else
8264 initializer = NULL;
8265
8266 /* A new-expression may not appear in an integral constant
8267 expression. */
8268 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8269 ret = error_mark_node;
8270 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8271 of a new-type-id or type-id of a new-expression, the new-expression shall
8272 contain a new-initializer of the form ( assignment-expression )".
8273 Additionally, consistently with the spirit of DR 1467, we want to accept
8274 'new auto { 2 }' too. */
8275 else if ((ret = type_uses_auto (type))
8276 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8277 && (vec_safe_length (initializer) != 1
8278 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8279 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8280 {
8281 error_at (token->location,
8282 "initialization of new-expression for type %<auto%> "
8283 "requires exactly one element");
8284 ret = error_mark_node;
8285 }
8286 else
8287 {
8288 /* Construct a location e.g.:
8289 ptr = new int[100]
8290 ^~~~~~~~~~~~
8291 with caret == start at the start of the "new" token, and the end
8292 at the end of the final token we consumed. */
8293 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8294 location_t end_loc = get_finish (end_tok->location);
8295 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8296
8297 /* Create a representation of the new-expression. */
8298 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8299 tf_warning_or_error);
8300 protected_set_expr_location (ret, combined_loc);
8301 }
8302
8303 if (placement != NULL)
8304 release_tree_vector (placement);
8305 if (initializer != NULL)
8306 release_tree_vector (initializer);
8307
8308 return ret;
8309}
8310
8311/* Parse a new-placement.
8312
8313 new-placement:
8314 ( expression-list )
8315
8316 Returns the same representation as for an expression-list. */
8317
8318static vec<tree, va_gc> *
8319cp_parser_new_placement (cp_parser* parser)
8320{
8321 vec<tree, va_gc> *expression_list;
8322
8323 /* Parse the expression-list. */
8324 expression_list = (cp_parser_parenthesized_expression_list
8325 (parser, non_attr, /*cast_p=*/false,
8326 /*allow_expansion_p=*/true,
8327 /*non_constant_p=*/NULL));
8328
8329 if (expression_list && expression_list->is_empty ())
8330 error ("expected expression-list or type-id");
8331
8332 return expression_list;
8333}
8334
8335/* Parse a new-type-id.
8336
8337 new-type-id:
8338 type-specifier-seq new-declarator [opt]
8339
8340 Returns the TYPE allocated. If the new-type-id indicates an array
8341 type, *NELTS is set to the number of elements in the last array
8342 bound; the TYPE will not include the last array bound. */
8343
8344static tree
8345cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8346{
8347 cp_decl_specifier_seq type_specifier_seq;
8348 cp_declarator *new_declarator;
8349 cp_declarator *declarator;
8350 cp_declarator *outer_declarator;
8351 const char *saved_message;
8352
8353 /* The type-specifier sequence must not contain type definitions.
8354 (It cannot contain declarations of new types either, but if they
8355 are not definitions we will catch that because they are not
8356 complete.) */
8357 saved_message = parser->type_definition_forbidden_message;
8358 parser->type_definition_forbidden_message
8359 = G_("types may not be defined in a new-type-id");
8360 /* Parse the type-specifier-seq. */
8361 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8362 /*is_trailing_return=*/false,
8363 &type_specifier_seq);
8364 /* Restore the old message. */
8365 parser->type_definition_forbidden_message = saved_message;
8366
8367 if (type_specifier_seq.type == error_mark_node)
8368 return error_mark_node;
8369
8370 /* Parse the new-declarator. */
8371 new_declarator = cp_parser_new_declarator_opt (parser);
8372
8373 /* Determine the number of elements in the last array dimension, if
8374 any. */
8375 *nelts = NULL_TREE;
8376 /* Skip down to the last array dimension. */
8377 declarator = new_declarator;
8378 outer_declarator = NULL;
8379 while (declarator && (declarator->kind == cdk_pointer
8380 || declarator->kind == cdk_ptrmem))
8381 {
8382 outer_declarator = declarator;
8383 declarator = declarator->declarator;
8384 }
8385 while (declarator
8386 && declarator->kind == cdk_array
8387 && declarator->declarator
8388 && declarator->declarator->kind == cdk_array)
8389 {
8390 outer_declarator = declarator;
8391 declarator = declarator->declarator;
8392 }
8393
8394 if (declarator && declarator->kind == cdk_array)
8395 {
8396 *nelts = declarator->u.array.bounds;
8397 if (*nelts == error_mark_node)
8398 *nelts = integer_one_node;
8399
8400 if (outer_declarator)
8401 outer_declarator->declarator = declarator->declarator;
8402 else
8403 new_declarator = NULL;
8404 }
8405
8406 return groktypename (&type_specifier_seq, new_declarator, false);
8407}
8408
8409/* Parse an (optional) new-declarator.
8410
8411 new-declarator:
8412 ptr-operator new-declarator [opt]
8413 direct-new-declarator
8414
8415 Returns the declarator. */
8416
8417static cp_declarator *
8418cp_parser_new_declarator_opt (cp_parser* parser)
8419{
8420 enum tree_code code;
8421 tree type, std_attributes = NULL_TREE;
8422 cp_cv_quals cv_quals;
8423
8424 /* We don't know if there's a ptr-operator next, or not. */
8425 cp_parser_parse_tentatively (parser);
8426 /* Look for a ptr-operator. */
8427 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8428 /* If that worked, look for more new-declarators. */
8429 if (cp_parser_parse_definitely (parser))
8430 {
8431 cp_declarator *declarator;
8432
8433 /* Parse another optional declarator. */
8434 declarator = cp_parser_new_declarator_opt (parser);
8435
8436 declarator = cp_parser_make_indirect_declarator
8437 (code, type, cv_quals, declarator, std_attributes);
8438
8439 return declarator;
8440 }
8441
8442 /* If the next token is a `[', there is a direct-new-declarator. */
8443 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8444 return cp_parser_direct_new_declarator (parser);
8445
8446 return NULL;
8447}
8448
8449/* Parse a direct-new-declarator.
8450
8451 direct-new-declarator:
8452 [ expression ]
8453 direct-new-declarator [constant-expression]
8454
8455 */
8456
8457static cp_declarator *
8458cp_parser_direct_new_declarator (cp_parser* parser)
8459{
8460 cp_declarator *declarator = NULL;
8461
8462 while (true)
8463 {
8464 tree expression;
8465 cp_token *token;
8466
8467 /* Look for the opening `['. */
8468 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8469
8470 token = cp_lexer_peek_token (parser->lexer);
8471 expression = cp_parser_expression (parser);
8472 /* The standard requires that the expression have integral
8473 type. DR 74 adds enumeration types. We believe that the
8474 real intent is that these expressions be handled like the
8475 expression in a `switch' condition, which also allows
8476 classes with a single conversion to integral or
8477 enumeration type. */
8478 if (!processing_template_decl)
8479 {
8480 expression
8481 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8482 expression,
8483 /*complain=*/true);
8484 if (!expression)
8485 {
8486 error_at (token->location,
8487 "expression in new-declarator must have integral "
8488 "or enumeration type");
8489 expression = error_mark_node;
8490 }
8491 }
8492
8493 /* Look for the closing `]'. */
8494 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8495
8496 /* Add this bound to the declarator. */
8497 declarator = make_array_declarator (declarator, expression);
8498
8499 /* If the next token is not a `[', then there are no more
8500 bounds. */
8501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8502 break;
8503 }
8504
8505 return declarator;
8506}
8507
8508/* Parse a new-initializer.
8509
8510 new-initializer:
8511 ( expression-list [opt] )
8512 braced-init-list
8513
8514 Returns a representation of the expression-list. */
8515
8516static vec<tree, va_gc> *
8517cp_parser_new_initializer (cp_parser* parser)
8518{
8519 vec<tree, va_gc> *expression_list;
8520
8521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8522 {
8523 tree t;
8524 bool expr_non_constant_p;
8525 cp_lexer_set_source_position (parser->lexer);
8526 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8527 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8528 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8529 expression_list = make_tree_vector_single (t);
8530 }
8531 else
8532 expression_list = (cp_parser_parenthesized_expression_list
8533 (parser, non_attr, /*cast_p=*/false,
8534 /*allow_expansion_p=*/true,
8535 /*non_constant_p=*/NULL));
8536
8537 return expression_list;
8538}
8539
8540/* Parse a delete-expression.
8541
8542 delete-expression:
8543 :: [opt] delete cast-expression
8544 :: [opt] delete [ ] cast-expression
8545
8546 Returns a representation of the expression. */
8547
8548static tree
8549cp_parser_delete_expression (cp_parser* parser)
8550{
8551 bool global_scope_p;
8552 bool array_p;
8553 tree expression;
8554
8555 /* Look for the optional `::' operator. */
8556 global_scope_p
8557 = (cp_parser_global_scope_opt (parser,
8558 /*current_scope_valid_p=*/false)
8559 != NULL_TREE);
8560 /* Look for the `delete' keyword. */
8561 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8562 /* See if the array syntax is in use. */
8563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8564 {
8565 /* Consume the `[' token. */
8566 cp_lexer_consume_token (parser->lexer);
8567 /* Look for the `]' token. */
8568 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8569 /* Remember that this is the `[]' construct. */
8570 array_p = true;
8571 }
8572 else
8573 array_p = false;
8574
8575 /* Parse the cast-expression. */
8576 expression = cp_parser_simple_cast_expression (parser);
8577
8578 /* A delete-expression may not appear in an integral constant
8579 expression. */
8580 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8581 return error_mark_node;
8582
8583 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8584 tf_warning_or_error);
8585}
8586
8587/* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8588 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8589 0 otherwise. */
8590
8591static int
8592cp_parser_tokens_start_cast_expression (cp_parser *parser)
8593{
8594 cp_token *token = cp_lexer_peek_token (parser->lexer);
8595 switch (token->type)
8596 {
8597 case CPP_COMMA:
8598 case CPP_SEMICOLON:
8599 case CPP_QUERY:
8600 case CPP_COLON:
8601 case CPP_CLOSE_SQUARE:
8602 case CPP_CLOSE_PAREN:
8603 case CPP_CLOSE_BRACE:
8604 case CPP_OPEN_BRACE:
8605 case CPP_DOT:
8606 case CPP_DOT_STAR:
8607 case CPP_DEREF:
8608 case CPP_DEREF_STAR:
8609 case CPP_DIV:
8610 case CPP_MOD:
8611 case CPP_LSHIFT:
8612 case CPP_RSHIFT:
8613 case CPP_LESS:
8614 case CPP_GREATER:
8615 case CPP_LESS_EQ:
8616 case CPP_GREATER_EQ:
8617 case CPP_EQ_EQ:
8618 case CPP_NOT_EQ:
8619 case CPP_EQ:
8620 case CPP_MULT_EQ:
8621 case CPP_DIV_EQ:
8622 case CPP_MOD_EQ:
8623 case CPP_PLUS_EQ:
8624 case CPP_MINUS_EQ:
8625 case CPP_RSHIFT_EQ:
8626 case CPP_LSHIFT_EQ:
8627 case CPP_AND_EQ:
8628 case CPP_XOR_EQ:
8629 case CPP_OR_EQ:
8630 case CPP_XOR:
8631 case CPP_OR:
8632 case CPP_OR_OR:
8633 case CPP_EOF:
8634 case CPP_ELLIPSIS:
8635 return 0;
8636
8637 case CPP_OPEN_PAREN:
8638 /* In ((type ()) () the last () isn't a valid cast-expression,
8639 so the whole must be parsed as postfix-expression. */
8640 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8641 != CPP_CLOSE_PAREN;
8642
8643 case CPP_OPEN_SQUARE:
8644 /* '[' may start a primary-expression in obj-c++ and in C++11,
8645 as a lambda-expression, eg, '(void)[]{}'. */
8646 if (cxx_dialect >= cxx11)
8647 return -1;
8648 return c_dialect_objc ();
8649
8650 case CPP_PLUS_PLUS:
8651 case CPP_MINUS_MINUS:
8652 /* '++' and '--' may or may not start a cast-expression:
8653
8654 struct T { void operator++(int); };
8655 void f() { (T())++; }
8656
8657 vs
8658
8659 int a;
8660 (int)++a; */
8661 return -1;
8662
8663 default:
8664 return 1;
8665 }
8666}
8667
8668/* Parse a cast-expression.
8669
8670 cast-expression:
8671 unary-expression
8672 ( type-id ) cast-expression
8673
8674 ADDRESS_P is true iff the unary-expression is appearing as the
8675 operand of the `&' operator. CAST_P is true if this expression is
8676 the target of a cast.
8677
8678 Returns a representation of the expression. */
8679
8680static cp_expr
8681cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8682 bool decltype_p, cp_id_kind * pidk)
8683{
8684 /* If it's a `(', then we might be looking at a cast. */
8685 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8686 {
8687 tree type = NULL_TREE;
8688 cp_expr expr (NULL_TREE);
8689 int cast_expression = 0;
8690 const char *saved_message;
8691
8692 /* There's no way to know yet whether or not this is a cast.
8693 For example, `(int (3))' is a unary-expression, while `(int)
8694 3' is a cast. So, we resort to parsing tentatively. */
8695 cp_parser_parse_tentatively (parser);
8696 /* Types may not be defined in a cast. */
8697 saved_message = parser->type_definition_forbidden_message;
8698 parser->type_definition_forbidden_message
8699 = G_("types may not be defined in casts");
8700 /* Consume the `('. */
8701 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8702 location_t open_paren_loc = open_paren->location;
8703
8704 /* A very tricky bit is that `(struct S) { 3 }' is a
8705 compound-literal (which we permit in C++ as an extension).
8706 But, that construct is not a cast-expression -- it is a
8707 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8708 is legal; if the compound-literal were a cast-expression,
8709 you'd need an extra set of parentheses.) But, if we parse
8710 the type-id, and it happens to be a class-specifier, then we
8711 will commit to the parse at that point, because we cannot
8712 undo the action that is done when creating a new class. So,
8713 then we cannot back up and do a postfix-expression.
8714
8715 Another tricky case is the following (c++/29234):
8716
8717 struct S { void operator () (); };
8718
8719 void foo ()
8720 {
8721 ( S()() );
8722 }
8723
8724 As a type-id we parse the parenthesized S()() as a function
8725 returning a function, groktypename complains and we cannot
8726 back up in this case either.
8727
8728 Therefore, we scan ahead to the closing `)', and check to see
8729 if the tokens after the `)' can start a cast-expression. Otherwise
8730 we are dealing with an unary-expression, a postfix-expression
8731 or something else.
8732
8733 Yet another tricky case, in C++11, is the following (c++/54891):
8734
8735 (void)[]{};
8736
8737 The issue is that usually, besides the case of lambda-expressions,
8738 the parenthesized type-id cannot be followed by '[', and, eg, we
8739 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8740 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8741 we don't commit, we try a cast-expression, then an unary-expression.
8742
8743 Save tokens so that we can put them back. */
8744 cp_lexer_save_tokens (parser->lexer);
8745
8746 /* We may be looking at a cast-expression. */
8747 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8748 /*consume_paren=*/true))
8749 cast_expression
8750 = cp_parser_tokens_start_cast_expression (parser);
8751
8752 /* Roll back the tokens we skipped. */
8753 cp_lexer_rollback_tokens (parser->lexer);
8754 /* If we aren't looking at a cast-expression, simulate an error so
8755 that the call to cp_parser_error_occurred below returns true. */
8756 if (!cast_expression)
8757 cp_parser_simulate_error (parser);
8758 else
8759 {
8760 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8761 parser->in_type_id_in_expr_p = true;
8762 /* Look for the type-id. */
8763 type = cp_parser_type_id (parser);
8764 /* Look for the closing `)'. */
8765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8766 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8767 }
8768
8769 /* Restore the saved message. */
8770 parser->type_definition_forbidden_message = saved_message;
8771
8772 /* At this point this can only be either a cast or a
8773 parenthesized ctor such as `(T ())' that looks like a cast to
8774 function returning T. */
8775 if (!cp_parser_error_occurred (parser))
8776 {
8777 /* Only commit if the cast-expression doesn't start with
8778 '++', '--', or '[' in C++11. */
8779 if (cast_expression > 0)
8780 cp_parser_commit_to_topmost_tentative_parse (parser);
8781
8782 expr = cp_parser_cast_expression (parser,
8783 /*address_p=*/false,
8784 /*cast_p=*/true,
8785 /*decltype_p=*/false,
8786 pidk);
8787
8788 if (cp_parser_parse_definitely (parser))
8789 {
8790 /* Warn about old-style casts, if so requested. */
8791 if (warn_old_style_cast
8792 && !in_system_header_at (input_location)
8793 && !VOID_TYPE_P (type)
8794 && current_lang_name != lang_name_c)
8795 warning (OPT_Wold_style_cast, "use of old-style cast");
8796
8797 /* Only type conversions to integral or enumeration types
8798 can be used in constant-expressions. */
8799 if (!cast_valid_in_integral_constant_expression_p (type)
8800 && cp_parser_non_integral_constant_expression (parser,
8801 NIC_CAST))
8802 return error_mark_node;
8803
8804 /* Perform the cast. */
8805 /* Make a location:
8806 (TYPE) EXPR
8807 ^~~~~~~~~~~
8808 with start==caret at the open paren, extending to the
8809 end of "expr". */
8810 location_t cast_loc = make_location (open_paren_loc,
8811 open_paren_loc,
8812 expr.get_finish ());
8813 expr = build_c_cast (cast_loc, type, expr);
8814 return expr;
8815 }
8816 }
8817 else
8818 cp_parser_abort_tentative_parse (parser);
8819 }
8820
8821 /* If we get here, then it's not a cast, so it must be a
8822 unary-expression. */
8823 return cp_parser_unary_expression (parser, pidk, address_p,
8824 cast_p, decltype_p);
8825}
8826
8827/* Parse a binary expression of the general form:
8828
8829 pm-expression:
8830 cast-expression
8831 pm-expression .* cast-expression
8832 pm-expression ->* cast-expression
8833
8834 multiplicative-expression:
8835 pm-expression
8836 multiplicative-expression * pm-expression
8837 multiplicative-expression / pm-expression
8838 multiplicative-expression % pm-expression
8839
8840 additive-expression:
8841 multiplicative-expression
8842 additive-expression + multiplicative-expression
8843 additive-expression - multiplicative-expression
8844
8845 shift-expression:
8846 additive-expression
8847 shift-expression << additive-expression
8848 shift-expression >> additive-expression
8849
8850 relational-expression:
8851 shift-expression
8852 relational-expression < shift-expression
8853 relational-expression > shift-expression
8854 relational-expression <= shift-expression
8855 relational-expression >= shift-expression
8856
8857 GNU Extension:
8858
8859 relational-expression:
8860 relational-expression <? shift-expression
8861 relational-expression >? shift-expression
8862
8863 equality-expression:
8864 relational-expression
8865 equality-expression == relational-expression
8866 equality-expression != relational-expression
8867
8868 and-expression:
8869 equality-expression
8870 and-expression & equality-expression
8871
8872 exclusive-or-expression:
8873 and-expression
8874 exclusive-or-expression ^ and-expression
8875
8876 inclusive-or-expression:
8877 exclusive-or-expression
8878 inclusive-or-expression | exclusive-or-expression
8879
8880 logical-and-expression:
8881 inclusive-or-expression
8882 logical-and-expression && inclusive-or-expression
8883
8884 logical-or-expression:
8885 logical-and-expression
8886 logical-or-expression || logical-and-expression
8887
8888 All these are implemented with a single function like:
8889
8890 binary-expression:
8891 simple-cast-expression
8892 binary-expression <token> binary-expression
8893
8894 CAST_P is true if this expression is the target of a cast.
8895
8896 The binops_by_token map is used to get the tree codes for each <token> type.
8897 binary-expressions are associated according to a precedence table. */
8898
8899#define TOKEN_PRECEDENCE(token) \
8900(((token->type == CPP_GREATER \
8901 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8902 && !parser->greater_than_is_operator_p) \
8903 ? PREC_NOT_OPERATOR \
8904 : binops_by_token[token->type].prec)
8905
8906static cp_expr
8907cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8908 bool no_toplevel_fold_p,
8909 bool decltype_p,
8910 enum cp_parser_prec prec,
8911 cp_id_kind * pidk)
8912{
8913 cp_parser_expression_stack stack;
8914 cp_parser_expression_stack_entry *sp = &stack[0];
8915 cp_parser_expression_stack_entry current;
8916 cp_expr rhs;
8917 cp_token *token;
8918 enum tree_code rhs_type;
8919 enum cp_parser_prec new_prec, lookahead_prec;
8920 tree overload;
8921
8922 /* Parse the first expression. */
8923 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8924 ? TRUTH_NOT_EXPR : ERROR_MARK);
8925 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8926 cast_p, decltype_p, pidk);
8927 current.prec = prec;
8928
8929 if (cp_parser_error_occurred (parser))
8930 return error_mark_node;
8931
8932 for (;;)
8933 {
8934 /* Get an operator token. */
8935 token = cp_lexer_peek_token (parser->lexer);
8936
8937 if (warn_cxx11_compat
8938 && token->type == CPP_RSHIFT
8939 && !parser->greater_than_is_operator_p)
8940 {
8941 if (warning_at (token->location, OPT_Wc__11_compat,
8942 "%<>>%> operator is treated"
8943 " as two right angle brackets in C++11"))
8944 inform (token->location,
8945 "suggest parentheses around %<>>%> expression");
8946 }
8947
8948 new_prec = TOKEN_PRECEDENCE (token);
8949 if (new_prec != PREC_NOT_OPERATOR
8950 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8951 /* This is a fold-expression; handle it later. */
8952 new_prec = PREC_NOT_OPERATOR;
8953
8954 /* Popping an entry off the stack means we completed a subexpression:
8955 - either we found a token which is not an operator (`>' where it is not
8956 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8957 will happen repeatedly;
8958 - or, we found an operator which has lower priority. This is the case
8959 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8960 parsing `3 * 4'. */
8961 if (new_prec <= current.prec)
8962 {
8963 if (sp == stack)
8964 break;
8965 else
8966 goto pop;
8967 }
8968
8969 get_rhs:
8970 current.tree_type = binops_by_token[token->type].tree_type;
8971 current.loc = token->location;
8972
8973 /* We used the operator token. */
8974 cp_lexer_consume_token (parser->lexer);
8975
8976 /* For "false && x" or "true || x", x will never be executed;
8977 disable warnings while evaluating it. */
8978 if (current.tree_type == TRUTH_ANDIF_EXPR)
8979 c_inhibit_evaluation_warnings +=
8980 cp_fully_fold (current.lhs) == truthvalue_false_node;
8981 else if (current.tree_type == TRUTH_ORIF_EXPR)
8982 c_inhibit_evaluation_warnings +=
8983 cp_fully_fold (current.lhs) == truthvalue_true_node;
8984
8985 /* Extract another operand. It may be the RHS of this expression
8986 or the LHS of a new, higher priority expression. */
8987 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8988 ? TRUTH_NOT_EXPR : ERROR_MARK);
8989 rhs = cp_parser_simple_cast_expression (parser);
8990
8991 /* Get another operator token. Look up its precedence to avoid
8992 building a useless (immediately popped) stack entry for common
8993 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8994 token = cp_lexer_peek_token (parser->lexer);
8995 lookahead_prec = TOKEN_PRECEDENCE (token);
8996 if (lookahead_prec != PREC_NOT_OPERATOR
8997 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8998 lookahead_prec = PREC_NOT_OPERATOR;
8999 if (lookahead_prec > new_prec)
9000 {
9001 /* ... and prepare to parse the RHS of the new, higher priority
9002 expression. Since precedence levels on the stack are
9003 monotonically increasing, we do not have to care about
9004 stack overflows. */
9005 *sp = current;
9006 ++sp;
9007 current.lhs = rhs;
9008 current.lhs_type = rhs_type;
9009 current.prec = new_prec;
9010 new_prec = lookahead_prec;
9011 goto get_rhs;
9012
9013 pop:
9014 lookahead_prec = new_prec;
9015 /* If the stack is not empty, we have parsed into LHS the right side
9016 (`4' in the example above) of an expression we had suspended.
9017 We can use the information on the stack to recover the LHS (`3')
9018 from the stack together with the tree code (`MULT_EXPR'), and
9019 the precedence of the higher level subexpression
9020 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9021 which will be used to actually build the additive expression. */
9022 rhs = current.lhs;
9023 rhs_type = current.lhs_type;
9024 --sp;
9025 current = *sp;
9026 }
9027
9028 /* Undo the disabling of warnings done above. */
9029 if (current.tree_type == TRUTH_ANDIF_EXPR)
9030 c_inhibit_evaluation_warnings -=
9031 cp_fully_fold (current.lhs) == truthvalue_false_node;
9032 else if (current.tree_type == TRUTH_ORIF_EXPR)
9033 c_inhibit_evaluation_warnings -=
9034 cp_fully_fold (current.lhs) == truthvalue_true_node;
9035
9036 if (warn_logical_not_paren
9037 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9038 && current.lhs_type == TRUTH_NOT_EXPR
9039 /* Avoid warning for !!x == y. */
9040 && (TREE_CODE (current.lhs) != NE_EXPR
9041 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9042 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9043 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9044 /* Avoid warning for !b == y where b is boolean. */
9045 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9046 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9047 != BOOLEAN_TYPE))))
9048 /* Avoid warning for !!b == y where b is boolean. */
9049 && (!DECL_P (current.lhs)
9050 || TREE_TYPE (current.lhs) == NULL_TREE
9051 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9052 warn_logical_not_parentheses (current.loc, current.tree_type,
9053 current.lhs, maybe_constant_value (rhs));
9054
9055 overload = NULL;
9056
9057 location_t combined_loc = make_location (current.loc,
9058 current.lhs.get_start (),
9059 rhs.get_finish ());
9060
9061 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9062 ERROR_MARK for everything that is not a binary expression.
9063 This makes warn_about_parentheses miss some warnings that
9064 involve unary operators. For unary expressions we should
9065 pass the correct tree_code unless the unary expression was
9066 surrounded by parentheses.
9067 */
9068 if (no_toplevel_fold_p
9069 && lookahead_prec <= current.prec
9070 && sp == stack)
9071 {
9072 if (current.lhs == error_mark_node || rhs == error_mark_node)
9073 current.lhs = error_mark_node;
9074 else
9075 {
9076 current.lhs
9077 = build_min (current.tree_type,
9078 TREE_CODE_CLASS (current.tree_type)
9079 == tcc_comparison
9080 ? boolean_type_node : TREE_TYPE (current.lhs),
9081 current.lhs.get_value (), rhs.get_value ());
9082 SET_EXPR_LOCATION (current.lhs, combined_loc);
9083 }
9084 }
9085 else
9086 {
9087 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9088 current.lhs, current.lhs_type,
9089 rhs, rhs_type, &overload,
9090 complain_flags (decltype_p));
9091 /* TODO: build_x_binary_op doesn't always honor the location. */
9092 current.lhs.set_location (combined_loc);
9093 }
9094 current.lhs_type = current.tree_type;
9095
9096 /* If the binary operator required the use of an overloaded operator,
9097 then this expression cannot be an integral constant-expression.
9098 An overloaded operator can be used even if both operands are
9099 otherwise permissible in an integral constant-expression if at
9100 least one of the operands is of enumeration type. */
9101
9102 if (overload
9103 && cp_parser_non_integral_constant_expression (parser,
9104 NIC_OVERLOADED))
9105 return error_mark_node;
9106 }
9107
9108 return current.lhs;
9109}
9110
9111static cp_expr
9112cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9113 bool no_toplevel_fold_p,
9114 enum cp_parser_prec prec,
9115 cp_id_kind * pidk)
9116{
9117 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9118 /*decltype*/false, prec, pidk);
9119}
9120
9121/* Parse the `? expression : assignment-expression' part of a
9122 conditional-expression. The LOGICAL_OR_EXPR is the
9123 logical-or-expression that started the conditional-expression.
9124 Returns a representation of the entire conditional-expression.
9125
9126 This routine is used by cp_parser_assignment_expression.
9127
9128 ? expression : assignment-expression
9129
9130 GNU Extensions:
9131
9132 ? : assignment-expression */
9133
9134static tree
9135cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9136{
9137 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9138 cp_expr assignment_expr;
9139 struct cp_token *token;
9140 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9141
9142 /* Consume the `?' token. */
9143 cp_lexer_consume_token (parser->lexer);
9144 token = cp_lexer_peek_token (parser->lexer);
9145 if (cp_parser_allow_gnu_extensions_p (parser)
9146 && token->type == CPP_COLON)
9147 {
9148 pedwarn (token->location, OPT_Wpedantic,
9149 "ISO C++ does not allow ?: with omitted middle operand");
9150 /* Implicit true clause. */
9151 expr = NULL_TREE;
9152 c_inhibit_evaluation_warnings +=
9153 folded_logical_or_expr == truthvalue_true_node;
9154 warn_for_omitted_condop (token->location, logical_or_expr);
9155 }
9156 else
9157 {
9158 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9159 parser->colon_corrects_to_scope_p = false;
9160 /* Parse the expression. */
9161 c_inhibit_evaluation_warnings +=
9162 folded_logical_or_expr == truthvalue_false_node;
9163 expr = cp_parser_expression (parser);
9164 c_inhibit_evaluation_warnings +=
9165 ((folded_logical_or_expr == truthvalue_true_node)
9166 - (folded_logical_or_expr == truthvalue_false_node));
9167 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9168 }
9169
9170 /* The next token should be a `:'. */
9171 cp_parser_require (parser, CPP_COLON, RT_COLON);
9172 /* Parse the assignment-expression. */
9173 assignment_expr = cp_parser_assignment_expression (parser);
9174 c_inhibit_evaluation_warnings -=
9175 folded_logical_or_expr == truthvalue_true_node;
9176
9177 /* Make a location:
9178 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9179 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9180 with the caret at the "?", ranging from the start of
9181 the logical_or_expr to the end of the assignment_expr. */
9182 loc = make_location (loc,
9183 logical_or_expr.get_start (),
9184 assignment_expr.get_finish ());
9185
9186 /* Build the conditional-expression. */
9187 return build_x_conditional_expr (loc, logical_or_expr,
9188 expr,
9189 assignment_expr,
9190 tf_warning_or_error);
9191}
9192
9193/* Parse an assignment-expression.
9194
9195 assignment-expression:
9196 conditional-expression
9197 logical-or-expression assignment-operator assignment_expression
9198 throw-expression
9199
9200 CAST_P is true if this expression is the target of a cast.
9201 DECLTYPE_P is true if this expression is the operand of decltype.
9202
9203 Returns a representation for the expression. */
9204
9205static cp_expr
9206cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9207 bool cast_p, bool decltype_p)
9208{
9209 cp_expr expr;
9210
9211 /* If the next token is the `throw' keyword, then we're looking at
9212 a throw-expression. */
9213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9214 expr = cp_parser_throw_expression (parser);
9215 /* Otherwise, it must be that we are looking at a
9216 logical-or-expression. */
9217 else
9218 {
9219 /* Parse the binary expressions (logical-or-expression). */
9220 expr = cp_parser_binary_expression (parser, cast_p, false,
9221 decltype_p,
9222 PREC_NOT_OPERATOR, pidk);
9223 /* If the next token is a `?' then we're actually looking at a
9224 conditional-expression. */
9225 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9226 return cp_parser_question_colon_clause (parser, expr);
9227 else
9228 {
9229 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9230
9231 /* If it's an assignment-operator, we're using the second
9232 production. */
9233 enum tree_code assignment_operator
9234 = cp_parser_assignment_operator_opt (parser);
9235 if (assignment_operator != ERROR_MARK)
9236 {
9237 bool non_constant_p;
9238
9239 /* Parse the right-hand side of the assignment. */
9240 cp_expr rhs = cp_parser_initializer_clause (parser,
9241 &non_constant_p);
9242
9243 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9244 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9245
9246 /* An assignment may not appear in a
9247 constant-expression. */
9248 if (cp_parser_non_integral_constant_expression (parser,
9249 NIC_ASSIGNMENT))
9250 return error_mark_node;
9251 /* Build the assignment expression. Its default
9252 location:
9253 LHS = RHS
9254 ~~~~^~~~~
9255 is the location of the '=' token as the
9256 caret, ranging from the start of the lhs to the
9257 end of the rhs. */
9258 loc = make_location (loc,
9259 expr.get_start (),
9260 rhs.get_finish ());
9261 expr = build_x_modify_expr (loc, expr,
9262 assignment_operator,
9263 rhs,
9264 complain_flags (decltype_p));
9265 /* TODO: build_x_modify_expr doesn't honor the location,
9266 so we must set it here. */
9267 expr.set_location (loc);
9268 }
9269 }
9270 }
9271
9272 return expr;
9273}
9274
9275/* Parse an (optional) assignment-operator.
9276
9277 assignment-operator: one of
9278 = *= /= %= += -= >>= <<= &= ^= |=
9279
9280 GNU Extension:
9281
9282 assignment-operator: one of
9283 <?= >?=
9284
9285 If the next token is an assignment operator, the corresponding tree
9286 code is returned, and the token is consumed. For example, for
9287 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9288 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9289 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9290 operator, ERROR_MARK is returned. */
9291
9292static enum tree_code
9293cp_parser_assignment_operator_opt (cp_parser* parser)
9294{
9295 enum tree_code op;
9296 cp_token *token;
9297
9298 /* Peek at the next token. */
9299 token = cp_lexer_peek_token (parser->lexer);
9300
9301 switch (token->type)
9302 {
9303 case CPP_EQ:
9304 op = NOP_EXPR;
9305 break;
9306
9307 case CPP_MULT_EQ:
9308 op = MULT_EXPR;
9309 break;
9310
9311 case CPP_DIV_EQ:
9312 op = TRUNC_DIV_EXPR;
9313 break;
9314
9315 case CPP_MOD_EQ:
9316 op = TRUNC_MOD_EXPR;
9317 break;
9318
9319 case CPP_PLUS_EQ:
9320 op = PLUS_EXPR;
9321 break;
9322
9323 case CPP_MINUS_EQ:
9324 op = MINUS_EXPR;
9325 break;
9326
9327 case CPP_RSHIFT_EQ:
9328 op = RSHIFT_EXPR;
9329 break;
9330
9331 case CPP_LSHIFT_EQ:
9332 op = LSHIFT_EXPR;
9333 break;
9334
9335 case CPP_AND_EQ:
9336 op = BIT_AND_EXPR;
9337 break;
9338
9339 case CPP_XOR_EQ:
9340 op = BIT_XOR_EXPR;
9341 break;
9342
9343 case CPP_OR_EQ:
9344 op = BIT_IOR_EXPR;
9345 break;
9346
9347 default:
9348 /* Nothing else is an assignment operator. */
9349 op = ERROR_MARK;
9350 }
9351
9352 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9353 if (op != ERROR_MARK
9354 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9355 op = ERROR_MARK;
9356
9357 /* If it was an assignment operator, consume it. */
9358 if (op != ERROR_MARK)
9359 cp_lexer_consume_token (parser->lexer);
9360
9361 return op;
9362}
9363
9364/* Parse an expression.
9365
9366 expression:
9367 assignment-expression
9368 expression , assignment-expression
9369
9370 CAST_P is true if this expression is the target of a cast.
9371 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9372 except possibly parenthesized or on the RHS of a comma (N3276).
9373
9374 Returns a representation of the expression. */
9375
9376static cp_expr
9377cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9378 bool cast_p, bool decltype_p)
9379{
9380 cp_expr expression = NULL_TREE;
9381 location_t loc = UNKNOWN_LOCATION;
9382
9383 while (true)
9384 {
9385 cp_expr assignment_expression;
9386
9387 /* Parse the next assignment-expression. */
9388 assignment_expression
9389 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9390
9391 /* We don't create a temporary for a call that is the immediate operand
9392 of decltype or on the RHS of a comma. But when we see a comma, we
9393 need to create a temporary for a call on the LHS. */
9394 if (decltype_p && !processing_template_decl
9395 && TREE_CODE (assignment_expression) == CALL_EXPR
9396 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9397 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9398 assignment_expression
9399 = build_cplus_new (TREE_TYPE (assignment_expression),
9400 assignment_expression, tf_warning_or_error);
9401
9402 /* If this is the first assignment-expression, we can just
9403 save it away. */
9404 if (!expression)
9405 expression = assignment_expression;
9406 else
9407 {
9408 /* Create a location with caret at the comma, ranging
9409 from the start of the LHS to the end of the RHS. */
9410 loc = make_location (loc,
9411 expression.get_start (),
9412 assignment_expression.get_finish ());
9413 expression = build_x_compound_expr (loc, expression,
9414 assignment_expression,
9415 complain_flags (decltype_p));
9416 expression.set_location (loc);
9417 }
9418 /* If the next token is not a comma, or we're in a fold-expression, then
9419 we are done with the expression. */
9420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9421 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9422 break;
9423 /* Consume the `,'. */
9424 loc = cp_lexer_peek_token (parser->lexer)->location;
9425 cp_lexer_consume_token (parser->lexer);
9426 /* A comma operator cannot appear in a constant-expression. */
9427 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9428 expression = error_mark_node;
9429 }
9430
9431 return expression;
9432}
9433
9434/* Parse a constant-expression.
9435
9436 constant-expression:
9437 conditional-expression
9438
9439 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9440 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9441 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9442 is false, NON_CONSTANT_P should be NULL. */
9443
9444static cp_expr
9445cp_parser_constant_expression (cp_parser* parser,
9446 bool allow_non_constant_p,
9447 bool *non_constant_p)
9448{
9449 bool saved_integral_constant_expression_p;
9450 bool saved_allow_non_integral_constant_expression_p;
9451 bool saved_non_integral_constant_expression_p;
9452 cp_expr expression;
9453
9454 /* It might seem that we could simply parse the
9455 conditional-expression, and then check to see if it were
9456 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9457 one that the compiler can figure out is constant, possibly after
9458 doing some simplifications or optimizations. The standard has a
9459 precise definition of constant-expression, and we must honor
9460 that, even though it is somewhat more restrictive.
9461
9462 For example:
9463
9464 int i[(2, 3)];
9465
9466 is not a legal declaration, because `(2, 3)' is not a
9467 constant-expression. The `,' operator is forbidden in a
9468 constant-expression. However, GCC's constant-folding machinery
9469 will fold this operation to an INTEGER_CST for `3'. */
9470
9471 /* Save the old settings. */
9472 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9473 saved_allow_non_integral_constant_expression_p
9474 = parser->allow_non_integral_constant_expression_p;
9475 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9476 /* We are now parsing a constant-expression. */
9477 parser->integral_constant_expression_p = true;
9478 parser->allow_non_integral_constant_expression_p
9479 = (allow_non_constant_p || cxx_dialect >= cxx11);
9480 parser->non_integral_constant_expression_p = false;
9481 /* Although the grammar says "conditional-expression", we parse an
9482 "assignment-expression", which also permits "throw-expression"
9483 and the use of assignment operators. In the case that
9484 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9485 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9486 actually essential that we look for an assignment-expression.
9487 For example, cp_parser_initializer_clauses uses this function to
9488 determine whether a particular assignment-expression is in fact
9489 constant. */
9490 expression = cp_parser_assignment_expression (parser);
9491 /* Restore the old settings. */
9492 parser->integral_constant_expression_p
9493 = saved_integral_constant_expression_p;
9494 parser->allow_non_integral_constant_expression_p
9495 = saved_allow_non_integral_constant_expression_p;
9496 if (cxx_dialect >= cxx11)
9497 {
9498 /* Require an rvalue constant expression here; that's what our
9499 callers expect. Reference constant expressions are handled
9500 separately in e.g. cp_parser_template_argument. */
9501 tree decay = expression;
9502 if (TREE_TYPE (expression)
9503 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9504 decay = build_address (expression);
9505 bool is_const = potential_rvalue_constant_expression (decay);
9506 parser->non_integral_constant_expression_p = !is_const;
9507 if (!is_const && !allow_non_constant_p)
9508 require_potential_rvalue_constant_expression (decay);
9509 }
9510 if (allow_non_constant_p)
9511 *non_constant_p = parser->non_integral_constant_expression_p;
9512 parser->non_integral_constant_expression_p
9513 = saved_non_integral_constant_expression_p;
9514
9515 return expression;
9516}
9517
9518/* Parse __builtin_offsetof.
9519
9520 offsetof-expression:
9521 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9522
9523 offsetof-member-designator:
9524 id-expression
9525 | offsetof-member-designator "." id-expression
9526 | offsetof-member-designator "[" expression "]"
9527 | offsetof-member-designator "->" id-expression */
9528
9529static cp_expr
9530cp_parser_builtin_offsetof (cp_parser *parser)
9531{
9532 int save_ice_p, save_non_ice_p;
9533 tree type;
9534 cp_expr expr;
9535 cp_id_kind dummy;
9536 cp_token *token;
9537 location_t finish_loc;
9538
9539 /* We're about to accept non-integral-constant things, but will
9540 definitely yield an integral constant expression. Save and
9541 restore these values around our local parsing. */
9542 save_ice_p = parser->integral_constant_expression_p;
9543 save_non_ice_p = parser->non_integral_constant_expression_p;
9544
9545 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9546
9547 /* Consume the "__builtin_offsetof" token. */
9548 cp_lexer_consume_token (parser->lexer);
9549 /* Consume the opening `('. */
9550 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9551 /* Parse the type-id. */
9552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9553 type = cp_parser_type_id (parser);
9554 /* Look for the `,'. */
9555 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9556 token = cp_lexer_peek_token (parser->lexer);
9557
9558 /* Build the (type *)null that begins the traditional offsetof macro. */
9559 tree object_ptr
9560 = build_static_cast (build_pointer_type (type), null_pointer_node,
9561 tf_warning_or_error);
9562
9563 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9564 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9565 true, &dummy, token->location);
9566 while (true)
9567 {
9568 token = cp_lexer_peek_token (parser->lexer);
9569 switch (token->type)
9570 {
9571 case CPP_OPEN_SQUARE:
9572 /* offsetof-member-designator "[" expression "]" */
9573 expr = cp_parser_postfix_open_square_expression (parser, expr,
9574 true, false);
9575 break;
9576
9577 case CPP_DEREF:
9578 /* offsetof-member-designator "->" identifier */
9579 expr = grok_array_decl (token->location, expr,
9580 integer_zero_node, false);
9581 /* FALLTHRU */
9582
9583 case CPP_DOT:
9584 /* offsetof-member-designator "." identifier */
9585 cp_lexer_consume_token (parser->lexer);
9586 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9587 expr, true, &dummy,
9588 token->location);
9589 break;
9590
9591 case CPP_CLOSE_PAREN:
9592 /* Consume the ")" token. */
9593 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9594 cp_lexer_consume_token (parser->lexer);
9595 goto success;
9596
9597 default:
9598 /* Error. We know the following require will fail, but
9599 that gives the proper error message. */
9600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9601 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9602 expr = error_mark_node;
9603 goto failure;
9604 }
9605 }
9606
9607 success:
9608 /* Make a location of the form:
9609 __builtin_offsetof (struct s, f)
9610 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9611 with caret at the type-id, ranging from the start of the
9612 "_builtin_offsetof" token to the close paren. */
9613 loc = make_location (loc, start_loc, finish_loc);
9614 /* The result will be an INTEGER_CST, so we need to explicitly
9615 preserve the location. */
9616 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9617
9618 failure:
9619 parser->integral_constant_expression_p = save_ice_p;
9620 parser->non_integral_constant_expression_p = save_non_ice_p;
9621
9622 return expr;
9623}
9624
9625/* Parse a trait expression.
9626
9627 Returns a representation of the expression, the underlying type
9628 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9629
9630static tree
9631cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9632{
9633 cp_trait_kind kind;
9634 tree type1, type2 = NULL_TREE;
9635 bool binary = false;
9636 bool variadic = false;
9637
9638 switch (keyword)
9639 {
9640 case RID_HAS_NOTHROW_ASSIGN:
9641 kind = CPTK_HAS_NOTHROW_ASSIGN;
9642 break;
9643 case RID_HAS_NOTHROW_CONSTRUCTOR:
9644 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9645 break;
9646 case RID_HAS_NOTHROW_COPY:
9647 kind = CPTK_HAS_NOTHROW_COPY;
9648 break;
9649 case RID_HAS_TRIVIAL_ASSIGN:
9650 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9651 break;
9652 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9653 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9654 break;
9655 case RID_HAS_TRIVIAL_COPY:
9656 kind = CPTK_HAS_TRIVIAL_COPY;
9657 break;
9658 case RID_HAS_TRIVIAL_DESTRUCTOR:
9659 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9660 break;
9661 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9662 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9663 break;
9664 case RID_HAS_VIRTUAL_DESTRUCTOR:
9665 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9666 break;
9667 case RID_IS_ABSTRACT:
9668 kind = CPTK_IS_ABSTRACT;
9669 break;
9670 case RID_IS_AGGREGATE:
9671 kind = CPTK_IS_AGGREGATE;
9672 break;
9673 case RID_IS_BASE_OF:
9674 kind = CPTK_IS_BASE_OF;
9675 binary = true;
9676 break;
9677 case RID_IS_CLASS:
9678 kind = CPTK_IS_CLASS;
9679 break;
9680 case RID_IS_EMPTY:
9681 kind = CPTK_IS_EMPTY;
9682 break;
9683 case RID_IS_ENUM:
9684 kind = CPTK_IS_ENUM;
9685 break;
9686 case RID_IS_FINAL:
9687 kind = CPTK_IS_FINAL;
9688 break;
9689 case RID_IS_LITERAL_TYPE:
9690 kind = CPTK_IS_LITERAL_TYPE;
9691 break;
9692 case RID_IS_POD:
9693 kind = CPTK_IS_POD;
9694 break;
9695 case RID_IS_POLYMORPHIC:
9696 kind = CPTK_IS_POLYMORPHIC;
9697 break;
9698 case RID_IS_SAME_AS:
9699 kind = CPTK_IS_SAME_AS;
9700 binary = true;
9701 break;
9702 case RID_IS_STD_LAYOUT:
9703 kind = CPTK_IS_STD_LAYOUT;
9704 break;
9705 case RID_IS_TRIVIAL:
9706 kind = CPTK_IS_TRIVIAL;
9707 break;
9708 case RID_IS_TRIVIALLY_ASSIGNABLE:
9709 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9710 binary = true;
9711 break;
9712 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9713 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9714 variadic = true;
9715 break;
9716 case RID_IS_TRIVIALLY_COPYABLE:
9717 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9718 break;
9719 case RID_IS_UNION:
9720 kind = CPTK_IS_UNION;
9721 break;
9722 case RID_UNDERLYING_TYPE:
9723 kind = CPTK_UNDERLYING_TYPE;
9724 break;
9725 case RID_BASES:
9726 kind = CPTK_BASES;
9727 break;
9728 case RID_DIRECT_BASES:
9729 kind = CPTK_DIRECT_BASES;
9730 break;
9731 default:
9732 gcc_unreachable ();
9733 }
9734
9735 /* Consume the token. */
9736 cp_lexer_consume_token (parser->lexer);
9737
9738 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9739
9740 {
9741 type_id_in_expr_sentinel s (parser);
9742 type1 = cp_parser_type_id (parser);
9743 }
9744
9745 if (type1 == error_mark_node)
9746 return error_mark_node;
9747
9748 if (binary)
9749 {
9750 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9751
9752 {
9753 type_id_in_expr_sentinel s (parser);
9754 type2 = cp_parser_type_id (parser);
9755 }
9756
9757 if (type2 == error_mark_node)
9758 return error_mark_node;
9759 }
9760 else if (variadic)
9761 {
9762 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9763 {
9764 cp_lexer_consume_token (parser->lexer);
9765 tree elt = cp_parser_type_id (parser);
9766 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9767 {
9768 cp_lexer_consume_token (parser->lexer);
9769 elt = make_pack_expansion (elt);
9770 }
9771 if (elt == error_mark_node)
9772 return error_mark_node;
9773 type2 = tree_cons (NULL_TREE, elt, type2);
9774 }
9775 }
9776
9777 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9778
9779 /* Complete the trait expression, which may mean either processing
9780 the trait expr now or saving it for template instantiation. */
9781 switch (kind)
9782 {
9783 case CPTK_UNDERLYING_TYPE:
9784 return finish_underlying_type (type1);
9785 case CPTK_BASES:
9786 return finish_bases (type1, false);
9787 case CPTK_DIRECT_BASES:
9788 return finish_bases (type1, true);
9789 default:
9790 return finish_trait_expr (kind, type1, type2);
9791 }
9792}
9793
9794/* Lambdas that appear in variable initializer or default argument scope
9795 get that in their mangling, so we need to record it. We might as well
9796 use the count for function and namespace scopes as well. */
9797static GTY(()) tree lambda_scope;
9798static GTY(()) int lambda_count;
9799struct GTY(()) tree_int
9800{
9801 tree t;
9802 int i;
9803};
9804static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9805
9806static void
9807start_lambda_scope (tree decl)
9808{
9809 tree_int ti;
9810 gcc_assert (decl);
9811 /* Once we're inside a function, we ignore other scopes and just push
9812 the function again so that popping works properly. */
9813 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9814 decl = current_function_decl;
9815 ti.t = lambda_scope;
9816 ti.i = lambda_count;
9817 vec_safe_push (lambda_scope_stack, ti);
9818 if (lambda_scope != decl)
9819 {
9820 /* Don't reset the count if we're still in the same function. */
9821 lambda_scope = decl;
9822 lambda_count = 0;
9823 }
9824}
9825
9826static void
9827record_lambda_scope (tree lambda)
9828{
9829 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9830 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9831}
9832
9833static void
9834finish_lambda_scope (void)
9835{
9836 tree_int *p = &lambda_scope_stack->last ();
9837 if (lambda_scope != p->t)
9838 {
9839 lambda_scope = p->t;
9840 lambda_count = p->i;
9841 }
9842 lambda_scope_stack->pop ();
9843}
9844
9845/* Parse a lambda expression.
9846
9847 lambda-expression:
9848 lambda-introducer lambda-declarator [opt] compound-statement
9849
9850 Returns a representation of the expression. */
9851
9852static cp_expr
9853cp_parser_lambda_expression (cp_parser* parser)
9854{
9855 tree lambda_expr = build_lambda_expr ();
9856 tree type;
9857 bool ok = true;
9858 cp_token *token = cp_lexer_peek_token (parser->lexer);
9859 cp_token_position start = 0;
9860
9861 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9862
9863 if (cp_unevaluated_operand)
9864 {
9865 if (!token->error_reported)
9866 {
9867 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9868 "lambda-expression in unevaluated context");
9869 token->error_reported = true;
9870 }
9871 ok = false;
9872 }
9873 else if (parser->in_template_argument_list_p)
9874 {
9875 if (!token->error_reported)
9876 {
9877 error_at (token->location, "lambda-expression in template-argument");
9878 token->error_reported = true;
9879 }
9880 ok = false;
9881 }
9882
9883 /* We may be in the middle of deferred access check. Disable
9884 it now. */
9885 push_deferring_access_checks (dk_no_deferred);
9886
9887 cp_parser_lambda_introducer (parser, lambda_expr);
9888
9889 type = begin_lambda_type (lambda_expr);
9890 if (type == error_mark_node)
9891 return error_mark_node;
9892
9893 record_lambda_scope (lambda_expr);
9894
9895 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9896 determine_visibility (TYPE_NAME (type));
9897
9898 /* Now that we've started the type, add the capture fields for any
9899 explicit captures. */
9900 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9901
9902 {
9903 /* Inside the class, surrounding template-parameter-lists do not apply. */
9904 unsigned int saved_num_template_parameter_lists
9905 = parser->num_template_parameter_lists;
9906 unsigned char in_statement = parser->in_statement;
9907 bool in_switch_statement_p = parser->in_switch_statement_p;
9908 bool fully_implicit_function_template_p
9909 = parser->fully_implicit_function_template_p;
9910 tree implicit_template_parms = parser->implicit_template_parms;
9911 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9912 bool auto_is_implicit_function_template_parm_p
9913 = parser->auto_is_implicit_function_template_parm_p;
9914
9915 parser->num_template_parameter_lists = 0;
9916 parser->in_statement = 0;
9917 parser->in_switch_statement_p = false;
9918 parser->fully_implicit_function_template_p = false;
9919 parser->implicit_template_parms = 0;
9920 parser->implicit_template_scope = 0;
9921 parser->auto_is_implicit_function_template_parm_p = false;
9922
9923 /* By virtue of defining a local class, a lambda expression has access to
9924 the private variables of enclosing classes. */
9925
9926 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9927
9928 if (ok && cp_parser_error_occurred (parser))
9929 ok = false;
9930
9931 if (ok)
9932 {
9933 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9934 && cp_parser_start_tentative_firewall (parser))
9935 start = token;
9936 cp_parser_lambda_body (parser, lambda_expr);
9937 }
9938 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9939 {
9940 if (cp_parser_skip_to_closing_brace (parser))
9941 cp_lexer_consume_token (parser->lexer);
9942 }
9943
9944 /* The capture list was built up in reverse order; fix that now. */
9945 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9946 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9947
9948 if (ok)
9949 maybe_add_lambda_conv_op (type);
9950
9951 type = finish_struct (type, /*attributes=*/NULL_TREE);
9952
9953 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9954 parser->in_statement = in_statement;
9955 parser->in_switch_statement_p = in_switch_statement_p;
9956 parser->fully_implicit_function_template_p
9957 = fully_implicit_function_template_p;
9958 parser->implicit_template_parms = implicit_template_parms;
9959 parser->implicit_template_scope = implicit_template_scope;
9960 parser->auto_is_implicit_function_template_parm_p
9961 = auto_is_implicit_function_template_parm_p;
9962 }
9963
9964 /* This field is only used during parsing of the lambda. */
9965 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9966
9967 /* This lambda shouldn't have any proxies left at this point. */
9968 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9969 /* And now that we're done, push proxies for an enclosing lambda. */
9970 insert_pending_capture_proxies ();
9971
9972 if (ok)
9973 lambda_expr = build_lambda_object (lambda_expr);
9974 else
9975 lambda_expr = error_mark_node;
9976
9977 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9978
9979 pop_deferring_access_checks ();
9980
9981 return lambda_expr;
9982}
9983
9984/* Parse the beginning of a lambda expression.
9985
9986 lambda-introducer:
9987 [ lambda-capture [opt] ]
9988
9989 LAMBDA_EXPR is the current representation of the lambda expression. */
9990
9991static void
9992cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9993{
9994 /* Need commas after the first capture. */
9995 bool first = true;
9996
9997 /* Eat the leading `['. */
9998 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9999
10000 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10001 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10002 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10003 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10004 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10005 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10006
10007 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10008 {
10009 cp_lexer_consume_token (parser->lexer);
10010 first = false;
10011 }
10012
10013 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10014 {
10015 cp_token* capture_token;
10016 tree capture_id;
10017 tree capture_init_expr;
10018 cp_id_kind idk = CP_ID_KIND_NONE;
10019 bool explicit_init_p = false;
10020
10021 enum capture_kind_type
10022 {
10023 BY_COPY,
10024 BY_REFERENCE
10025 };
10026 enum capture_kind_type capture_kind = BY_COPY;
10027
10028 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10029 {
10030 error ("expected end of capture-list");
10031 return;
10032 }
10033
10034 if (first)
10035 first = false;
10036 else
10037 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10038
10039 /* Possibly capture `this'. */
10040 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10041 {
10042 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10043 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10044 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10045 "with by-copy capture default");
10046 cp_lexer_consume_token (parser->lexer);
10047 add_capture (lambda_expr,
10048 /*id=*/this_identifier,
10049 /*initializer=*/finish_this_expr (),
10050 /*by_reference_p=*/true,
10051 explicit_init_p);
10052 continue;
10053 }
10054
10055 /* Possibly capture `*this'. */
10056 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10057 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10058 {
10059 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10060 if (cxx_dialect < cxx1z)
10061 pedwarn (loc, 0, "%<*this%> capture only available with "
10062 "-std=c++1z or -std=gnu++1z");
10063 cp_lexer_consume_token (parser->lexer);
10064 cp_lexer_consume_token (parser->lexer);
10065 add_capture (lambda_expr,
10066 /*id=*/this_identifier,
10067 /*initializer=*/finish_this_expr (),
10068 /*by_reference_p=*/false,
10069 explicit_init_p);
10070 continue;
10071 }
10072
10073 /* Remember whether we want to capture as a reference or not. */
10074 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10075 {
10076 capture_kind = BY_REFERENCE;
10077 cp_lexer_consume_token (parser->lexer);
10078 }
10079
10080 /* Get the identifier. */
10081 capture_token = cp_lexer_peek_token (parser->lexer);
10082 capture_id = cp_parser_identifier (parser);
10083
10084 if (capture_id == error_mark_node)
10085 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10086 delimiters, but I modified this to stop on unnested ']' as well. It
10087 was already changed to stop on unnested '}', so the
10088 "closing_parenthesis" name is no more misleading with my change. */
10089 {
10090 cp_parser_skip_to_closing_parenthesis (parser,
10091 /*recovering=*/true,
10092 /*or_comma=*/true,
10093 /*consume_paren=*/true);
10094 break;
10095 }
10096
10097 /* Find the initializer for this capture. */
10098 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10099 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10100 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10101 {
10102 bool direct, non_constant;
10103 /* An explicit initializer exists. */
10104 if (cxx_dialect < cxx14)
10105 pedwarn (input_location, 0,
10106 "lambda capture initializers "
10107 "only available with -std=c++14 or -std=gnu++14");
10108 capture_init_expr = cp_parser_initializer (parser, &direct,
10109 &non_constant);
10110 explicit_init_p = true;
10111 if (capture_init_expr == NULL_TREE)
10112 {
10113 error ("empty initializer for lambda init-capture");
10114 capture_init_expr = error_mark_node;
10115 }
10116 }
10117 else
10118 {
10119 const char* error_msg;
10120
10121 /* Turn the identifier into an id-expression. */
10122 capture_init_expr
10123 = cp_parser_lookup_name_simple (parser, capture_id,
10124 capture_token->location);
10125
10126 if (capture_init_expr == error_mark_node)
10127 {
10128 unqualified_name_lookup_error (capture_id);
10129 continue;
10130 }
10131 else if (DECL_P (capture_init_expr)
10132 && (!VAR_P (capture_init_expr)
10133 && TREE_CODE (capture_init_expr) != PARM_DECL))
10134 {
10135 error_at (capture_token->location,
10136 "capture of non-variable %qD ",
10137 capture_init_expr);
10138 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10139 "%q#D declared here", capture_init_expr);
10140 continue;
10141 }
10142 if (VAR_P (capture_init_expr)
10143 && decl_storage_duration (capture_init_expr) != dk_auto)
10144 {
10145 if (pedwarn (capture_token->location, 0, "capture of variable "
10146 "%qD with non-automatic storage duration",
10147 capture_init_expr))
10148 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10149 "%q#D declared here", capture_init_expr);
10150 continue;
10151 }
10152
10153 capture_init_expr
10154 = finish_id_expression
10155 (capture_id,
10156 capture_init_expr,
10157 parser->scope,
10158 &idk,
10159 /*integral_constant_expression_p=*/false,
10160 /*allow_non_integral_constant_expression_p=*/false,
10161 /*non_integral_constant_expression_p=*/NULL,
10162 /*template_p=*/false,
10163 /*done=*/true,
10164 /*address_p=*/false,
10165 /*template_arg_p=*/false,
10166 &error_msg,
10167 capture_token->location);
10168
10169 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10170 {
10171 cp_lexer_consume_token (parser->lexer);
10172 capture_init_expr = make_pack_expansion (capture_init_expr);
10173 }
10174 else
10175 check_for_bare_parameter_packs (capture_init_expr);
10176 }
10177
10178 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10179 && !explicit_init_p)
10180 {
10181 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10182 && capture_kind == BY_COPY)
10183 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10184 "of %qD redundant with by-copy capture default",
10185 capture_id);
10186 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10187 && capture_kind == BY_REFERENCE)
10188 pedwarn (capture_token->location, 0, "explicit by-reference "
10189 "capture of %qD redundant with by-reference capture "
10190 "default", capture_id);
10191 }
10192
10193 add_capture (lambda_expr,
10194 capture_id,
10195 capture_init_expr,
10196 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10197 explicit_init_p);
10198 }
10199
10200 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10201}
10202
10203/* Parse the (optional) middle of a lambda expression.
10204
10205 lambda-declarator:
10206 < template-parameter-list [opt] >
10207 ( parameter-declaration-clause [opt] )
10208 attribute-specifier [opt]
10209 decl-specifier-seq [opt]
10210 exception-specification [opt]
10211 lambda-return-type-clause [opt]
10212
10213 LAMBDA_EXPR is the current representation of the lambda expression. */
10214
10215static bool
10216cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10217{
10218 /* 5.1.1.4 of the standard says:
10219 If a lambda-expression does not include a lambda-declarator, it is as if
10220 the lambda-declarator were ().
10221 This means an empty parameter list, no attributes, and no exception
10222 specification. */
10223 tree param_list = void_list_node;
10224 tree attributes = NULL_TREE;
10225 tree exception_spec = NULL_TREE;
10226 tree template_param_list = NULL_TREE;
10227 tree tx_qual = NULL_TREE;
10228 cp_decl_specifier_seq lambda_specs;
10229 clear_decl_specs (&lambda_specs);
10230
10231 /* The template-parameter-list is optional, but must begin with
10232 an opening angle if present. */
10233 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10234 {
10235 if (cxx_dialect < cxx14)
10236 pedwarn (parser->lexer->next_token->location, 0,
10237 "lambda templates are only available with "
10238 "-std=c++14 or -std=gnu++14");
10239 else
10240 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10241 "ISO C++ does not support lambda templates");
10242
10243 cp_lexer_consume_token (parser->lexer);
10244
10245 template_param_list = cp_parser_template_parameter_list (parser);
10246
10247 cp_parser_skip_to_end_of_template_parameter_list (parser);
10248
10249 /* We just processed one more parameter list. */
10250 ++parser->num_template_parameter_lists;
10251 }
10252
10253 /* The parameter-declaration-clause is optional (unless
10254 template-parameter-list was given), but must begin with an
10255 opening parenthesis if present. */
10256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10257 {
10258 cp_lexer_consume_token (parser->lexer);
10259
10260 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10261
10262 /* Parse parameters. */
10263 param_list = cp_parser_parameter_declaration_clause (parser);
10264
10265 /* Default arguments shall not be specified in the
10266 parameter-declaration-clause of a lambda-declarator. */
10267 if (cxx_dialect < cxx14)
10268 for (tree t = param_list; t; t = TREE_CHAIN (t))
10269 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10270 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10271 "default argument specified for lambda parameter");
10272
10273 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10274
10275 attributes = cp_parser_attributes_opt (parser);
10276
10277 /* In the decl-specifier-seq of the lambda-declarator, each
10278 decl-specifier shall either be mutable or constexpr. */
10279 int declares_class_or_enum;
10280 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10281 cp_parser_decl_specifier_seq (parser,
10282 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10283 &lambda_specs, &declares_class_or_enum);
10284 if (lambda_specs.storage_class == sc_mutable)
10285 {
10286 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10287 if (lambda_specs.conflicting_specifiers_p)
10288 error_at (lambda_specs.locations[ds_storage_class],
10289 "duplicate %<mutable%>");
10290 }
10291
10292 tx_qual = cp_parser_tx_qualifier_opt (parser);
10293
10294 /* Parse optional exception specification. */
10295 exception_spec = cp_parser_exception_specification_opt (parser);
10296
10297 /* Parse optional trailing return type. */
10298 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10299 {
10300 cp_lexer_consume_token (parser->lexer);
10301 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10302 = cp_parser_trailing_type_id (parser);
10303 }
10304
10305 /* The function parameters must be in scope all the way until after the
10306 trailing-return-type in case of decltype. */
10307 pop_bindings_and_leave_scope ();
10308 }
10309 else if (template_param_list != NULL_TREE) // generate diagnostic
10310 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10311
10312 /* Create the function call operator.
10313
10314 Messing with declarators like this is no uglier than building up the
10315 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10316 other code. */
10317 {
10318 cp_decl_specifier_seq return_type_specs;
10319 cp_declarator* declarator;
10320 tree fco;
10321 int quals;
10322 void *p;
10323
10324 clear_decl_specs (&return_type_specs);
10325 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10326 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10327 else
10328 /* Maybe we will deduce the return type later. */
10329 return_type_specs.type = make_auto ();
10330
10331 if (lambda_specs.locations[ds_constexpr])
10332 {
10333 if (cxx_dialect >= cxx1z)
10334 return_type_specs.locations[ds_constexpr]
10335 = lambda_specs.locations[ds_constexpr];
10336 else
10337 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10338 "lambda only available with -std=c++1z or -std=gnu++1z");
10339 }
10340
10341 p = obstack_alloc (&declarator_obstack, 0);
10342
10343 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10344 sfk_none);
10345
10346 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10347 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10348 declarator = make_call_declarator (declarator, param_list, quals,
10349 VIRT_SPEC_UNSPECIFIED,
10350 REF_QUAL_NONE,
10351 tx_qual,
10352 exception_spec,
10353 /*late_return_type=*/NULL_TREE,
10354 /*requires_clause*/NULL_TREE);
10355 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10356
10357 fco = grokmethod (&return_type_specs,
10358 declarator,
10359 attributes);
10360 if (fco != error_mark_node)
10361 {
10362 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10363 DECL_ARTIFICIAL (fco) = 1;
10364 /* Give the object parameter a different name. */
10365 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10366 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10367 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10368 }
10369 if (template_param_list)
10370 {
10371 fco = finish_member_template_decl (fco);
10372 finish_template_decl (template_param_list);
10373 --parser->num_template_parameter_lists;
10374 }
10375 else if (parser->fully_implicit_function_template_p)
10376 fco = finish_fully_implicit_template (parser, fco);
10377
10378 finish_member_declaration (fco);
10379
10380 obstack_free (&declarator_obstack, p);
10381
10382 return (fco != error_mark_node);
10383 }
10384}
10385
10386/* Parse the body of a lambda expression, which is simply
10387
10388 compound-statement
10389
10390 but which requires special handling.
10391 LAMBDA_EXPR is the current representation of the lambda expression. */
10392
10393static void
10394cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10395{
10396 bool nested = (current_function_decl != NULL_TREE);
10397 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10398 if (nested)
10399 push_function_context ();
10400 else
10401 /* Still increment function_depth so that we don't GC in the
10402 middle of an expression. */
10403 ++function_depth;
10404 vec<tree> omp_privatization_save;
10405 save_omp_privatization_clauses (omp_privatization_save);
10406 /* Clear this in case we're in the middle of a default argument. */
10407 parser->local_variables_forbidden_p = false;
10408
10409 /* Finish the function call operator
10410 - class_specifier
10411 + late_parsing_for_member
10412 + function_definition_after_declarator
10413 + ctor_initializer_opt_and_function_body */
10414 {
10415 tree fco = lambda_function (lambda_expr);
10416 tree body;
10417 bool done = false;
10418 tree compound_stmt;
10419 tree cap;
10420
10421 /* Let the front end know that we are going to be defining this
10422 function. */
10423 start_preparsed_function (fco,
10424 NULL_TREE,
10425 SF_PRE_PARSED | SF_INCLASS_INLINE);
10426
10427 start_lambda_scope (fco);
10428 body = begin_function_body ();
10429
10430 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10431 goto out;
10432
10433 /* Push the proxies for any explicit captures. */
10434 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10435 cap = TREE_CHAIN (cap))
10436 build_capture_proxy (TREE_PURPOSE (cap));
10437
10438 compound_stmt = begin_compound_stmt (0);
10439
10440 /* 5.1.1.4 of the standard says:
10441 If a lambda-expression does not include a trailing-return-type, it
10442 is as if the trailing-return-type denotes the following type:
10443 * if the compound-statement is of the form
10444 { return attribute-specifier [opt] expression ; }
10445 the type of the returned expression after lvalue-to-rvalue
10446 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10447 (_conv.array_ 4.2), and function-to-pointer conversion
10448 (_conv.func_ 4.3);
10449 * otherwise, void. */
10450
10451 /* In a lambda that has neither a lambda-return-type-clause
10452 nor a deducible form, errors should be reported for return statements
10453 in the body. Since we used void as the placeholder return type, parsing
10454 the body as usual will give such desired behavior. */
10455 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10456 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10457 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10458 {
10459 tree expr = NULL_TREE;
10460 cp_id_kind idk = CP_ID_KIND_NONE;
10461
10462 /* Parse tentatively in case there's more after the initial return
10463 statement. */
10464 cp_parser_parse_tentatively (parser);
10465
10466 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10467
10468 expr = cp_parser_expression (parser, &idk);
10469
10470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10471 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10472
10473 if (cp_parser_parse_definitely (parser))
10474 {
10475 if (!processing_template_decl)
10476 {
10477 tree type = lambda_return_type (expr);
10478 apply_deduced_return_type (fco, type);
10479 if (type == error_mark_node)
10480 expr = error_mark_node;
10481 }
10482
10483 /* Will get error here if type not deduced yet. */
10484 finish_return_stmt (expr);
10485
10486 done = true;
10487 }
10488 }
10489
10490 if (!done)
10491 {
10492 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10493 cp_parser_label_declaration (parser);
10494 cp_parser_statement_seq_opt (parser, NULL_TREE);
10495 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10496 }
10497
10498 finish_compound_stmt (compound_stmt);
10499
10500 out:
10501 finish_function_body (body);
10502 finish_lambda_scope ();
10503
10504 /* Finish the function and generate code for it if necessary. */
10505 tree fn = finish_function (/*inline*/2);
10506
10507 /* Only expand if the call op is not a template. */
10508 if (!DECL_TEMPLATE_INFO (fco))
10509 expand_or_defer_fn (fn);
10510 }
10511
10512 restore_omp_privatization_clauses (omp_privatization_save);
10513 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10514 if (nested)
10515 pop_function_context();
10516 else
10517 --function_depth;
10518}
10519
10520/* Statements [gram.stmt.stmt] */
10521
10522/* Parse a statement.
10523
10524 statement:
10525 labeled-statement
10526 expression-statement
10527 compound-statement
10528 selection-statement
10529 iteration-statement
10530 jump-statement
10531 declaration-statement
10532 try-block
10533
10534 C++11:
10535
10536 statement:
10537 labeled-statement
10538 attribute-specifier-seq (opt) expression-statement
10539 attribute-specifier-seq (opt) compound-statement
10540 attribute-specifier-seq (opt) selection-statement
10541 attribute-specifier-seq (opt) iteration-statement
10542 attribute-specifier-seq (opt) jump-statement
10543 declaration-statement
10544 attribute-specifier-seq (opt) try-block
10545
10546 init-statement:
10547 expression-statement
10548 simple-declaration
10549
10550 TM Extension:
10551
10552 statement:
10553 atomic-statement
10554
10555 IN_COMPOUND is true when the statement is nested inside a
10556 cp_parser_compound_statement; this matters for certain pragmas.
10557
10558 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10559 is a (possibly labeled) if statement which is not enclosed in braces
10560 and has an else clause. This is used to implement -Wparentheses.
10561
10562 CHAIN is a vector of if-else-if conditions. */
10563
10564static void
10565cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10566 bool in_compound, bool *if_p, vec<tree> *chain)
10567{
10568 tree statement, std_attrs = NULL_TREE;
10569 cp_token *token;
10570 location_t statement_location, attrs_location;
10571
10572 restart:
10573 if (if_p != NULL)
10574 *if_p = false;
10575 /* There is no statement yet. */
10576 statement = NULL_TREE;
10577
10578 saved_token_sentinel saved_tokens (parser->lexer);
10579 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10580 if (c_dialect_objc ())
10581 /* In obj-c++, seeing '[[' might be the either the beginning of
10582 c++11 attributes, or a nested objc-message-expression. So
10583 let's parse the c++11 attributes tentatively. */
10584 cp_parser_parse_tentatively (parser);
10585 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10586 if (c_dialect_objc ())
10587 {
10588 if (!cp_parser_parse_definitely (parser))
10589 std_attrs = NULL_TREE;
10590 }
10591
10592 /* Peek at the next token. */
10593 token = cp_lexer_peek_token (parser->lexer);
10594 /* Remember the location of the first token in the statement. */
10595 statement_location = token->location;
10596 /* If this is a keyword, then that will often determine what kind of
10597 statement we have. */
10598 if (token->type == CPP_KEYWORD)
10599 {
10600 enum rid keyword = token->keyword;
10601
10602 switch (keyword)
10603 {
10604 case RID_CASE:
10605 case RID_DEFAULT:
10606 /* Looks like a labeled-statement with a case label.
10607 Parse the label, and then use tail recursion to parse
10608 the statement. */
10609 cp_parser_label_for_labeled_statement (parser, std_attrs);
10610 in_compound = false;
10611 goto restart;
10612
10613 case RID_IF:
10614 case RID_SWITCH:
10615 statement = cp_parser_selection_statement (parser, if_p, chain);
10616 break;
10617
10618 case RID_WHILE:
10619 case RID_DO:
10620 case RID_FOR:
10621 statement = cp_parser_iteration_statement (parser, if_p, false);
10622 break;
10623
10624 case RID_CILK_FOR:
10625 if (!flag_cilkplus)
10626 {
10627 error_at (cp_lexer_peek_token (parser->lexer)->location,
10628 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10629 cp_lexer_consume_token (parser->lexer);
10630 statement = error_mark_node;
10631 }
10632 else
10633 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10634 break;
10635
10636 case RID_BREAK:
10637 case RID_CONTINUE:
10638 case RID_RETURN:
10639 case RID_GOTO:
10640 statement = cp_parser_jump_statement (parser);
10641 break;
10642
10643 case RID_CILK_SYNC:
10644 cp_lexer_consume_token (parser->lexer);
10645 if (flag_cilkplus)
10646 {
10647 tree sync_expr = build_cilk_sync ();
10648 SET_EXPR_LOCATION (sync_expr,
10649 token->location);
10650 statement = finish_expr_stmt (sync_expr);
10651 }
10652 else
10653 {
10654 error_at (token->location, "-fcilkplus must be enabled to use"
10655 " %<_Cilk_sync%>");
10656 statement = error_mark_node;
10657 }
10658 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10659 break;
10660
10661 /* Objective-C++ exception-handling constructs. */
10662 case RID_AT_TRY:
10663 case RID_AT_CATCH:
10664 case RID_AT_FINALLY:
10665 case RID_AT_SYNCHRONIZED:
10666 case RID_AT_THROW:
10667 statement = cp_parser_objc_statement (parser);
10668 break;
10669
10670 case RID_TRY:
10671 statement = cp_parser_try_block (parser);
10672 break;
10673
10674 case RID_NAMESPACE:
10675 /* This must be a namespace alias definition. */
10676 cp_parser_declaration_statement (parser);
10677 return;
10678
10679 case RID_TRANSACTION_ATOMIC:
10680 case RID_TRANSACTION_RELAXED:
10681 case RID_SYNCHRONIZED:
10682 case RID_ATOMIC_NOEXCEPT:
10683 case RID_ATOMIC_CANCEL:
10684 statement = cp_parser_transaction (parser, token);
10685 break;
10686 case RID_TRANSACTION_CANCEL:
10687 statement = cp_parser_transaction_cancel (parser);
10688 break;
10689
10690 default:
10691 /* It might be a keyword like `int' that can start a
10692 declaration-statement. */
10693 break;
10694 }
10695 }
10696 else if (token->type == CPP_NAME)
10697 {
10698 /* If the next token is a `:', then we are looking at a
10699 labeled-statement. */
10700 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10701 if (token->type == CPP_COLON)
10702 {
10703 /* Looks like a labeled-statement with an ordinary label.
10704 Parse the label, and then use tail recursion to parse
10705 the statement. */
10706
10707 cp_parser_label_for_labeled_statement (parser, std_attrs);
10708 in_compound = false;
10709 goto restart;
10710 }
10711 }
10712 /* Anything that starts with a `{' must be a compound-statement. */
10713 else if (token->type == CPP_OPEN_BRACE)
10714 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10715 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10716 a statement all its own. */
10717 else if (token->type == CPP_PRAGMA)
10718 {
10719 /* Only certain OpenMP pragmas are attached to statements, and thus
10720 are considered statements themselves. All others are not. In
10721 the context of a compound, accept the pragma as a "statement" and
10722 return so that we can check for a close brace. Otherwise we
10723 require a real statement and must go back and read one. */
10724 if (in_compound)
10725 cp_parser_pragma (parser, pragma_compound, if_p);
10726 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10727 goto restart;
10728 return;
10729 }
10730 else if (token->type == CPP_EOF)
10731 {
10732 cp_parser_error (parser, "expected statement");
10733 return;
10734 }
10735
10736 /* Everything else must be a declaration-statement or an
10737 expression-statement. Try for the declaration-statement
10738 first, unless we are looking at a `;', in which case we know that
10739 we have an expression-statement. */
10740 if (!statement)
10741 {
10742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10743 {
10744 if (std_attrs != NULL_TREE)
10745 {
10746 /* Attributes should be parsed as part of the the
10747 declaration, so let's un-parse them. */
10748 saved_tokens.rollback();
10749 std_attrs = NULL_TREE;
10750 }
10751
10752 cp_parser_parse_tentatively (parser);
10753 /* Try to parse the declaration-statement. */
10754 cp_parser_declaration_statement (parser);
10755 /* If that worked, we're done. */
10756 if (cp_parser_parse_definitely (parser))
10757 return;
10758 }
10759 /* Look for an expression-statement instead. */
10760 statement = cp_parser_expression_statement (parser, in_statement_expr);
10761
10762 /* Handle [[fallthrough]];. */
10763 if (attribute_fallthrough_p (std_attrs))
10764 {
10765 /* The next token after the fallthrough attribute is ';'. */
10766 if (statement == NULL_TREE)
10767 {
10768 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10769 statement = build_call_expr_internal_loc (statement_location,
10770 IFN_FALLTHROUGH,
10771 void_type_node, 0);
10772 finish_expr_stmt (statement);
10773 }
10774 else
10775 warning_at (statement_location, OPT_Wattributes,
10776 "%<fallthrough%> attribute not followed by %<;%>");
10777 std_attrs = NULL_TREE;
10778 }
10779 }
10780
10781 /* Set the line number for the statement. */
10782 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10783 SET_EXPR_LOCATION (statement, statement_location);
10784
10785 /* Allow "[[fallthrough]];", but warn otherwise. */
10786 if (std_attrs != NULL_TREE)
10787 warning_at (attrs_location,
10788 OPT_Wattributes,
10789 "attributes at the beginning of statement are ignored");
10790}
10791
10792/* Append ATTR to attribute list ATTRS. */
10793
10794static tree
10795attr_chainon (tree attrs, tree attr)
10796{
10797 if (attrs == error_mark_node)
10798 return error_mark_node;
10799 if (attr == error_mark_node)
10800 return error_mark_node;
10801 return chainon (attrs, attr);
10802}
10803
10804/* Parse the label for a labeled-statement, i.e.
10805
10806 identifier :
10807 case constant-expression :
10808 default :
10809
10810 GNU Extension:
10811 case constant-expression ... constant-expression : statement
10812
10813 When a label is parsed without errors, the label is added to the
10814 parse tree by the finish_* functions, so this function doesn't
10815 have to return the label. */
10816
10817static void
10818cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10819{
10820 cp_token *token;
10821 tree label = NULL_TREE;
10822 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10823
10824 /* The next token should be an identifier. */
10825 token = cp_lexer_peek_token (parser->lexer);
10826 if (token->type != CPP_NAME
10827 && token->type != CPP_KEYWORD)
10828 {
10829 cp_parser_error (parser, "expected labeled-statement");
10830 return;
10831 }
10832
10833 /* Remember whether this case or a user-defined label is allowed to fall
10834 through to. */
10835 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10836
10837 parser->colon_corrects_to_scope_p = false;
10838 switch (token->keyword)
10839 {
10840 case RID_CASE:
10841 {
10842 tree expr, expr_hi;
10843 cp_token *ellipsis;
10844
10845 /* Consume the `case' token. */
10846 cp_lexer_consume_token (parser->lexer);
10847 /* Parse the constant-expression. */
10848 expr = cp_parser_constant_expression (parser);
10849 if (check_for_bare_parameter_packs (expr))
10850 expr = error_mark_node;
10851
10852 ellipsis = cp_lexer_peek_token (parser->lexer);
10853 if (ellipsis->type == CPP_ELLIPSIS)
10854 {
10855 /* Consume the `...' token. */
10856 cp_lexer_consume_token (parser->lexer);
10857 expr_hi = cp_parser_constant_expression (parser);
10858 if (check_for_bare_parameter_packs (expr_hi))
10859 expr_hi = error_mark_node;
10860
10861 /* We don't need to emit warnings here, as the common code
10862 will do this for us. */
10863 }
10864 else
10865 expr_hi = NULL_TREE;
10866
10867 if (parser->in_switch_statement_p)
10868 {
10869 tree l = finish_case_label (token->location, expr, expr_hi);
10870 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10871 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10872 }
10873 else
10874 error_at (token->location,
10875 "case label %qE not within a switch statement",
10876 expr);
10877 }
10878 break;
10879
10880 case RID_DEFAULT:
10881 /* Consume the `default' token. */
10882 cp_lexer_consume_token (parser->lexer);
10883
10884 if (parser->in_switch_statement_p)
10885 {
10886 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10887 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10888 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10889 }
10890 else
10891 error_at (token->location, "case label not within a switch statement");
10892 break;
10893
10894 default:
10895 /* Anything else must be an ordinary label. */
10896 label = finish_label_stmt (cp_parser_identifier (parser));
10897 if (label && TREE_CODE (label) == LABEL_DECL)
10898 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10899 break;
10900 }
10901
10902 /* Require the `:' token. */
10903 cp_parser_require (parser, CPP_COLON, RT_COLON);
10904
10905 /* An ordinary label may optionally be followed by attributes.
10906 However, this is only permitted if the attributes are then
10907 followed by a semicolon. This is because, for backward
10908 compatibility, when parsing
10909 lab: __attribute__ ((unused)) int i;
10910 we want the attribute to attach to "i", not "lab". */
10911 if (label != NULL_TREE
10912 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10913 {
10914 tree attrs;
10915 cp_parser_parse_tentatively (parser);
10916 attrs = cp_parser_gnu_attributes_opt (parser);
10917 if (attrs == NULL_TREE
10918 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10919 cp_parser_abort_tentative_parse (parser);
10920 else if (!cp_parser_parse_definitely (parser))
10921 ;
10922 else
10923 attributes = attr_chainon (attributes, attrs);
10924 }
10925
10926 if (attributes != NULL_TREE)
10927 cplus_decl_attributes (&label, attributes, 0);
10928
10929 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10930}
10931
10932/* Parse an expression-statement.
10933
10934 expression-statement:
10935 expression [opt] ;
10936
10937 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10938 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10939 indicates whether this expression-statement is part of an
10940 expression statement. */
10941
10942static tree
10943cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10944{
10945 tree statement = NULL_TREE;
10946 cp_token *token = cp_lexer_peek_token (parser->lexer);
10947 location_t loc = token->location;
10948
10949 /* There might be attribute fallthrough. */
10950 tree attr = cp_parser_gnu_attributes_opt (parser);
10951
10952 /* If the next token is a ';', then there is no expression
10953 statement. */
10954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10955 {
10956 statement = cp_parser_expression (parser);
10957 if (statement == error_mark_node
10958 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10959 {
10960 cp_parser_skip_to_end_of_block_or_statement (parser);
10961 return error_mark_node;
10962 }
10963 }
10964
10965 /* Handle [[fallthrough]];. */
10966 if (attribute_fallthrough_p (attr))
10967 {
10968 /* The next token after the fallthrough attribute is ';'. */
10969 if (statement == NULL_TREE)
10970 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10971 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10972 void_type_node, 0);
10973 else
10974 warning_at (loc, OPT_Wattributes,
10975 "%<fallthrough%> attribute not followed by %<;%>");
10976 attr = NULL_TREE;
10977 }
10978
10979 /* Allow "[[fallthrough]];", but warn otherwise. */
10980 if (attr != NULL_TREE)
10981 warning_at (loc, OPT_Wattributes,
10982 "attributes at the beginning of statement are ignored");
10983
10984 /* Give a helpful message for "A<T>::type t;" and the like. */
10985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10986 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10987 {
10988 if (TREE_CODE (statement) == SCOPE_REF)
10989 error_at (token->location, "need %<typename%> before %qE because "
10990 "%qT is a dependent scope",
10991 statement, TREE_OPERAND (statement, 0));
10992 else if (is_overloaded_fn (statement)
10993 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10994 {
10995 /* A::A a; */
10996 tree fn = get_first_fn (statement);
10997 error_at (token->location,
10998 "%<%T::%D%> names the constructor, not the type",
10999 DECL_CONTEXT (fn), DECL_NAME (fn));
11000 }
11001 }
11002
11003 /* Consume the final `;'. */
11004 cp_parser_consume_semicolon_at_end_of_statement (parser);
11005
11006 if (in_statement_expr
11007 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11008 /* This is the final expression statement of a statement
11009 expression. */
11010 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11011 else if (statement)
11012 statement = finish_expr_stmt (statement);
11013
11014 return statement;
11015}
11016
11017/* Parse a compound-statement.
11018
11019 compound-statement:
11020 { statement-seq [opt] }
11021
11022 GNU extension:
11023
11024 compound-statement:
11025 { label-declaration-seq [opt] statement-seq [opt] }
11026
11027 label-declaration-seq:
11028 label-declaration
11029 label-declaration-seq label-declaration
11030
11031 Returns a tree representing the statement. */
11032
11033static tree
11034cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11035 int bcs_flags, bool function_body)
11036{
11037 tree compound_stmt;
11038
11039 /* Consume the `{'. */
11040 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11041 return error_mark_node;
11042 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11043 && !function_body && cxx_dialect < cxx14)
11044 pedwarn (input_location, OPT_Wpedantic,
11045 "compound-statement in constexpr function");
11046 /* Begin the compound-statement. */
11047 compound_stmt = begin_compound_stmt (bcs_flags);
11048 /* If the next keyword is `__label__' we have a label declaration. */
11049 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11050 cp_parser_label_declaration (parser);
11051 /* Parse an (optional) statement-seq. */
11052 cp_parser_statement_seq_opt (parser, in_statement_expr);
11053 /* Finish the compound-statement. */
11054 finish_compound_stmt (compound_stmt);
11055 /* Consume the `}'. */
11056 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11057
11058 return compound_stmt;
11059}
11060
11061/* Parse an (optional) statement-seq.
11062
11063 statement-seq:
11064 statement
11065 statement-seq [opt] statement */
11066
11067static void
11068cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11069{
11070 /* Scan statements until there aren't any more. */
11071 while (true)
11072 {
11073 cp_token *token = cp_lexer_peek_token (parser->lexer);
11074
11075 /* If we are looking at a `}', then we have run out of
11076 statements; the same is true if we have reached the end
11077 of file, or have stumbled upon a stray '@end'. */
11078 if (token->type == CPP_CLOSE_BRACE
11079 || token->type == CPP_EOF
11080 || token->type == CPP_PRAGMA_EOL
11081 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11082 break;
11083
11084 /* If we are in a compound statement and find 'else' then
11085 something went wrong. */
11086 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11087 {
11088 if (parser->in_statement & IN_IF_STMT)
11089 break;
11090 else
11091 {
11092 token = cp_lexer_consume_token (parser->lexer);
11093 error_at (token->location, "%<else%> without a previous %<if%>");
11094 }
11095 }
11096
11097 /* Parse the statement. */
11098 cp_parser_statement (parser, in_statement_expr, true, NULL);
11099 }
11100}
11101
11102/* Return true if we're looking at (init; cond), false otherwise. */
11103
11104static bool
11105cp_parser_init_statement_p (cp_parser *parser)
11106{
11107 /* Save tokens so that we can put them back. */
11108 cp_lexer_save_tokens (parser->lexer);
11109
11110 /* Look for ';' that is not nested in () or {}. */
11111 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11112 /*recovering=*/false,
11113 CPP_SEMICOLON,
11114 /*consume_paren=*/false);
11115
11116 /* Roll back the tokens we skipped. */
11117 cp_lexer_rollback_tokens (parser->lexer);
11118
11119 return ret == -1;
11120}
11121
11122/* Parse a selection-statement.
11123
11124 selection-statement:
11125 if ( init-statement [opt] condition ) statement
11126 if ( init-statement [opt] condition ) statement else statement
11127 switch ( init-statement [opt] condition ) statement
11128
11129 Returns the new IF_STMT or SWITCH_STMT.
11130
11131 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11132 is a (possibly labeled) if statement which is not enclosed in
11133 braces and has an else clause. This is used to implement
11134 -Wparentheses.
11135
11136 CHAIN is a vector of if-else-if conditions. This is used to implement
11137 -Wduplicated-cond. */
11138
11139static tree
11140cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11141 vec<tree> *chain)
11142{
11143 cp_token *token;
11144 enum rid keyword;
11145 token_indent_info guard_tinfo;
11146
11147 if (if_p != NULL)
11148 *if_p = false;
11149
11150 /* Peek at the next token. */
11151 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11152 guard_tinfo = get_token_indent_info (token);
11153
11154 /* See what kind of keyword it is. */
11155 keyword = token->keyword;
11156 switch (keyword)
11157 {
11158 case RID_IF:
11159 case RID_SWITCH:
11160 {
11161 tree statement;
11162 tree condition;
11163
11164 bool cx = false;
11165 if (keyword == RID_IF
11166 && cp_lexer_next_token_is_keyword (parser->lexer,
11167 RID_CONSTEXPR))
11168 {
11169 cx = true;
11170 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11171 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11172 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11173 "with -std=c++1z or -std=gnu++1z");
11174 }
11175
11176 /* Look for the `('. */
11177 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11178 {
11179 cp_parser_skip_to_end_of_statement (parser);
11180 return error_mark_node;
11181 }
11182
11183 /* Begin the selection-statement. */
11184 if (keyword == RID_IF)
11185 {
11186 statement = begin_if_stmt ();
11187 IF_STMT_CONSTEXPR_P (statement) = cx;
11188 }
11189 else
11190 statement = begin_switch_stmt ();
11191
11192 /* Parse the optional init-statement. */
11193 if (cp_parser_init_statement_p (parser))
11194 {
11195 tree decl;
11196 if (cxx_dialect < cxx1z)
11197 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11198 "init-statement in selection statements only available "
11199 "with -std=c++1z or -std=gnu++1z");
11200 cp_parser_init_statement (parser, &decl);
11201 }
11202
11203 /* Parse the condition. */
11204 condition = cp_parser_condition (parser);
11205 /* Look for the `)'. */
11206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11207 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11208 /*consume_paren=*/true);
11209
11210 if (keyword == RID_IF)
11211 {
11212 bool nested_if;
11213 unsigned char in_statement;
11214
11215 /* Add the condition. */
11216 condition = finish_if_stmt_cond (condition, statement);
11217
11218 if (warn_duplicated_cond)
11219 warn_duplicated_cond_add_or_warn (token->location, condition,
11220 &chain);
11221
11222 /* Parse the then-clause. */
11223 in_statement = parser->in_statement;
11224 parser->in_statement |= IN_IF_STMT;
11225
11226 /* Outside a template, the non-selected branch of a constexpr
11227 if is a 'discarded statement', i.e. unevaluated. */
11228 bool was_discarded = in_discarded_stmt;
11229 bool discard_then = (cx && !processing_template_decl
11230 && integer_zerop (condition));
11231 if (discard_then)
11232 {
11233 in_discarded_stmt = true;
11234 ++c_inhibit_evaluation_warnings;
11235 }
11236
11237 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11238 guard_tinfo);
11239
11240 parser->in_statement = in_statement;
11241
11242 finish_then_clause (statement);
11243
11244 if (discard_then)
11245 {
11246 THEN_CLAUSE (statement) = NULL_TREE;
11247 in_discarded_stmt = was_discarded;
11248 --c_inhibit_evaluation_warnings;
11249 }
11250
11251 /* If the next token is `else', parse the else-clause. */
11252 if (cp_lexer_next_token_is_keyword (parser->lexer,
11253 RID_ELSE))
11254 {
11255 bool discard_else = (cx && !processing_template_decl
11256 && integer_nonzerop (condition));
11257 if (discard_else)
11258 {
11259 in_discarded_stmt = true;
11260 ++c_inhibit_evaluation_warnings;
11261 }
11262
11263 guard_tinfo
11264 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11265 /* Consume the `else' keyword. */
11266 cp_lexer_consume_token (parser->lexer);
11267 if (warn_duplicated_cond)
11268 {
11269 if (cp_lexer_next_token_is_keyword (parser->lexer,
11270 RID_IF)
11271 && chain == NULL)
11272 {
11273 /* We've got "if (COND) else if (COND2)". Start
11274 the condition chain and add COND as the first
11275 element. */
11276 chain = new vec<tree> ();
11277 if (!CONSTANT_CLASS_P (condition)
11278 && !TREE_SIDE_EFFECTS (condition))
11279 {
11280 /* Wrap it in a NOP_EXPR so that we can set the
11281 location of the condition. */
11282 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11283 condition);
11284 SET_EXPR_LOCATION (e, token->location);
11285 chain->safe_push (e);
11286 }
11287 }
11288 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11289 RID_IF))
11290 {
11291 /* This is if-else without subsequent if. Zap the
11292 condition chain; we would have already warned at
11293 this point. */
11294 delete chain;
11295 chain = NULL;
11296 }
11297 }
11298 begin_else_clause (statement);
11299 /* Parse the else-clause. */
11300 cp_parser_implicitly_scoped_statement (parser, NULL,
11301 guard_tinfo, chain);
11302
11303 finish_else_clause (statement);
11304
11305 /* If we are currently parsing a then-clause, then
11306 IF_P will not be NULL. We set it to true to
11307 indicate that this if statement has an else clause.
11308 This may trigger the Wparentheses warning below
11309 when we get back up to the parent if statement. */
11310 if (if_p != NULL)
11311 *if_p = true;
11312
11313 if (discard_else)
11314 {
11315 ELSE_CLAUSE (statement) = NULL_TREE;
11316 in_discarded_stmt = was_discarded;
11317 --c_inhibit_evaluation_warnings;
11318 }
11319 }
11320 else
11321 {
11322 /* This if statement does not have an else clause. If
11323 NESTED_IF is true, then the then-clause has an if
11324 statement which does have an else clause. We warn
11325 about the potential ambiguity. */
11326 if (nested_if)
11327 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11328 "suggest explicit braces to avoid ambiguous"
11329 " %<else%>");
11330 if (warn_duplicated_cond)
11331 {
11332 /* We don't need the condition chain anymore. */
11333 delete chain;
11334 chain = NULL;
11335 }
11336 }
11337
11338 /* Now we're all done with the if-statement. */
11339 finish_if_stmt (statement);
11340 }
11341 else
11342 {
11343 bool in_switch_statement_p;
11344 unsigned char in_statement;
11345
11346 /* Add the condition. */
11347 finish_switch_cond (condition, statement);
11348
11349 /* Parse the body of the switch-statement. */
11350 in_switch_statement_p = parser->in_switch_statement_p;
11351 in_statement = parser->in_statement;
11352 parser->in_switch_statement_p = true;
11353 parser->in_statement |= IN_SWITCH_STMT;
11354 cp_parser_implicitly_scoped_statement (parser, if_p,
11355 guard_tinfo);
11356 parser->in_switch_statement_p = in_switch_statement_p;
11357 parser->in_statement = in_statement;
11358
11359 /* Now we're all done with the switch-statement. */
11360 finish_switch_stmt (statement);
11361 }
11362
11363 return statement;
11364 }
11365 break;
11366
11367 default:
11368 cp_parser_error (parser, "expected selection-statement");
11369 return error_mark_node;
11370 }
11371}
11372
11373/* Parse a condition.
11374
11375 condition:
11376 expression
11377 type-specifier-seq declarator = initializer-clause
11378 type-specifier-seq declarator braced-init-list
11379
11380 GNU Extension:
11381
11382 condition:
11383 type-specifier-seq declarator asm-specification [opt]
11384 attributes [opt] = assignment-expression
11385
11386 Returns the expression that should be tested. */
11387
11388static tree
11389cp_parser_condition (cp_parser* parser)
11390{
11391 cp_decl_specifier_seq type_specifiers;
11392 const char *saved_message;
11393 int declares_class_or_enum;
11394
11395 /* Try the declaration first. */
11396 cp_parser_parse_tentatively (parser);
11397 /* New types are not allowed in the type-specifier-seq for a
11398 condition. */
11399 saved_message = parser->type_definition_forbidden_message;
11400 parser->type_definition_forbidden_message
11401 = G_("types may not be defined in conditions");
11402 /* Parse the type-specifier-seq. */
11403 cp_parser_decl_specifier_seq (parser,
11404 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11405 &type_specifiers,
11406 &declares_class_or_enum);
11407 /* Restore the saved message. */
11408 parser->type_definition_forbidden_message = saved_message;
11409 /* If all is well, we might be looking at a declaration. */
11410 if (!cp_parser_error_occurred (parser))
11411 {
11412 tree decl;
11413 tree asm_specification;
11414 tree attributes;
11415 cp_declarator *declarator;
11416 tree initializer = NULL_TREE;
11417
11418 /* Parse the declarator. */
11419 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11420 /*ctor_dtor_or_conv_p=*/NULL,
11421 /*parenthesized_p=*/NULL,
11422 /*member_p=*/false,
11423 /*friend_p=*/false);
11424 /* Parse the attributes. */
11425 attributes = cp_parser_attributes_opt (parser);
11426 /* Parse the asm-specification. */
11427 asm_specification = cp_parser_asm_specification_opt (parser);
11428 /* If the next token is not an `=' or '{', then we might still be
11429 looking at an expression. For example:
11430
11431 if (A(a).x)
11432
11433 looks like a decl-specifier-seq and a declarator -- but then
11434 there is no `=', so this is an expression. */
11435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11436 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11437 cp_parser_simulate_error (parser);
11438
11439 /* If we did see an `=' or '{', then we are looking at a declaration
11440 for sure. */
11441 if (cp_parser_parse_definitely (parser))
11442 {
11443 tree pushed_scope;
11444 bool non_constant_p;
11445 int flags = LOOKUP_ONLYCONVERTING;
11446
11447 /* Create the declaration. */
11448 decl = start_decl (declarator, &type_specifiers,
11449 /*initialized_p=*/true,
11450 attributes, /*prefix_attributes=*/NULL_TREE,
11451 &pushed_scope);
11452
11453 /* Parse the initializer. */
11454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11455 {
11456 initializer = cp_parser_braced_list (parser, &non_constant_p);
11457 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11458 flags = 0;
11459 }
11460 else
11461 {
11462 /* Consume the `='. */
11463 cp_parser_require (parser, CPP_EQ, RT_EQ);
11464 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11465 }
11466 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11467 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11468
11469 /* Process the initializer. */
11470 cp_finish_decl (decl,
11471 initializer, !non_constant_p,
11472 asm_specification,
11473 flags);
11474
11475 if (pushed_scope)
11476 pop_scope (pushed_scope);
11477
11478 return convert_from_reference (decl);
11479 }
11480 }
11481 /* If we didn't even get past the declarator successfully, we are
11482 definitely not looking at a declaration. */
11483 else
11484 cp_parser_abort_tentative_parse (parser);
11485
11486 /* Otherwise, we are looking at an expression. */
11487 return cp_parser_expression (parser);
11488}
11489
11490/* Parses a for-statement or range-for-statement until the closing ')',
11491 not included. */
11492
11493static tree
11494cp_parser_for (cp_parser *parser, bool ivdep)
11495{
11496 tree init, scope, decl;
11497 bool is_range_for;
11498
11499 /* Begin the for-statement. */
11500 scope = begin_for_scope (&init);
11501
11502 /* Parse the initialization. */
11503 is_range_for = cp_parser_init_statement (parser, &decl);
11504
11505 if (is_range_for)
11506 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11507 else
11508 return cp_parser_c_for (parser, scope, init, ivdep);
11509}
11510
11511static tree
11512cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11513{
11514 /* Normal for loop */
11515 tree condition = NULL_TREE;
11516 tree expression = NULL_TREE;
11517 tree stmt;
11518
11519 stmt = begin_for_stmt (scope, init);
11520 /* The init-statement has already been parsed in
11521 cp_parser_init_statement, so no work is needed here. */
11522 finish_init_stmt (stmt);
11523
11524 /* If there's a condition, process it. */
11525 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11526 condition = cp_parser_condition (parser);
11527 else if (ivdep)
11528 {
11529 cp_parser_error (parser, "missing loop condition in loop with "
11530 "%<GCC ivdep%> pragma");
11531 condition = error_mark_node;
11532 }
11533 finish_for_cond (condition, stmt, ivdep);
11534 /* Look for the `;'. */
11535 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11536
11537 /* If there's an expression, process it. */
11538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11539 expression = cp_parser_expression (parser);
11540 finish_for_expr (expression, stmt);
11541
11542 return stmt;
11543}
11544
11545/* Tries to parse a range-based for-statement:
11546
11547 range-based-for:
11548 decl-specifier-seq declarator : expression
11549
11550 The decl-specifier-seq declarator and the `:' are already parsed by
11551 cp_parser_init_statement. If processing_template_decl it returns a
11552 newly created RANGE_FOR_STMT; if not, it is converted to a
11553 regular FOR_STMT. */
11554
11555static tree
11556cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11557 bool ivdep)
11558{
11559 tree stmt, range_expr;
11560 auto_vec <cxx_binding *, 16> bindings;
11561 auto_vec <tree, 16> names;
11562 tree decomp_first_name = NULL_TREE;
11563 unsigned int decomp_cnt = 0;
11564
11565 /* Get the range declaration momentarily out of the way so that
11566 the range expression doesn't clash with it. */
11567 if (range_decl != error_mark_node)
11568 {
11569 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11570 {
11571 tree v = DECL_VALUE_EXPR (range_decl);
11572 /* For decomposition declaration get all of the corresponding
11573 declarations out of the way. */
11574 if (TREE_CODE (v) == ARRAY_REF
11575 && VAR_P (TREE_OPERAND (v, 0))
11576 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11577 {
11578 tree d = range_decl;
11579 range_decl = TREE_OPERAND (v, 0);
11580 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11581 decomp_first_name = d;
11582 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11583 {
11584 tree name = DECL_NAME (d);
11585 names.safe_push (name);
11586 bindings.safe_push (IDENTIFIER_BINDING (name));
11587 IDENTIFIER_BINDING (name)
11588 = IDENTIFIER_BINDING (name)->previous;
11589 }
11590 }
11591 }
11592 if (names.is_empty ())
11593 {
11594 tree name = DECL_NAME (range_decl);
11595 names.safe_push (name);
11596 bindings.safe_push (IDENTIFIER_BINDING (name));
11597 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11598 }
11599 }
11600
11601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11602 {
11603 bool expr_non_constant_p;
11604 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11605 }
11606 else
11607 range_expr = cp_parser_expression (parser);
11608
11609 /* Put the range declaration(s) back into scope. */
11610 for (unsigned int i = 0; i < names.length (); i++)
11611 {
11612 cxx_binding *binding = bindings[i];
11613 binding->previous = IDENTIFIER_BINDING (names[i]);
11614 IDENTIFIER_BINDING (names[i]) = binding;
11615 }
11616
11617 /* If in template, STMT is converted to a normal for-statement
11618 at instantiation. If not, it is done just ahead. */
11619 if (processing_template_decl)
11620 {
11621 if (check_for_bare_parameter_packs (range_expr))
11622 range_expr = error_mark_node;
11623 stmt = begin_range_for_stmt (scope, init);
11624 if (ivdep)
11625 RANGE_FOR_IVDEP (stmt) = 1;
11626 finish_range_for_decl (stmt, range_decl, range_expr);
11627 if (!type_dependent_expression_p (range_expr)
11628 /* do_auto_deduction doesn't mess with template init-lists. */
11629 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11630 do_range_for_auto_deduction (range_decl, range_expr);
11631 }
11632 else
11633 {
11634 stmt = begin_for_stmt (scope, init);
11635 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11636 decomp_first_name, decomp_cnt, ivdep);
11637 }
11638 return stmt;
11639}
11640
11641/* Subroutine of cp_convert_range_for: given the initializer expression,
11642 builds up the range temporary. */
11643
11644static tree
11645build_range_temp (tree range_expr)
11646{
11647 tree range_type, range_temp;
11648
11649 /* Find out the type deduced by the declaration
11650 `auto &&__range = range_expr'. */
11651 range_type = cp_build_reference_type (make_auto (), true);
11652 range_type = do_auto_deduction (range_type, range_expr,
11653 type_uses_auto (range_type));
11654
11655 /* Create the __range variable. */
11656 range_temp = build_decl (input_location, VAR_DECL,
11657 get_identifier ("__for_range"), range_type);
11658 TREE_USED (range_temp) = 1;
11659 DECL_ARTIFICIAL (range_temp) = 1;
11660
11661 return range_temp;
11662}
11663
11664/* Used by cp_parser_range_for in template context: we aren't going to
11665 do a full conversion yet, but we still need to resolve auto in the
11666 type of the for-range-declaration if present. This is basically
11667 a shortcut version of cp_convert_range_for. */
11668
11669static void
11670do_range_for_auto_deduction (tree decl, tree range_expr)
11671{
11672 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11673 if (auto_node)
11674 {
11675 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11676 range_temp = convert_from_reference (build_range_temp (range_expr));
11677 iter_type = (cp_parser_perform_range_for_lookup
11678 (range_temp, &begin_dummy, &end_dummy));
11679 if (iter_type)
11680 {
11681 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11682 iter_type);
11683 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11684 tf_warning_or_error);
11685 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11686 iter_decl, auto_node);
11687 }
11688 }
11689}
11690
11691/* Converts a range-based for-statement into a normal
11692 for-statement, as per the definition.
11693
11694 for (RANGE_DECL : RANGE_EXPR)
11695 BLOCK
11696
11697 should be equivalent to:
11698
11699 {
11700 auto &&__range = RANGE_EXPR;
11701 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11702 __begin != __end;
11703 ++__begin)
11704 {
11705 RANGE_DECL = *__begin;
11706 BLOCK
11707 }
11708 }
11709
11710 If RANGE_EXPR is an array:
11711 BEGIN_EXPR = __range
11712 END_EXPR = __range + ARRAY_SIZE(__range)
11713 Else if RANGE_EXPR has a member 'begin' or 'end':
11714 BEGIN_EXPR = __range.begin()
11715 END_EXPR = __range.end()
11716 Else:
11717 BEGIN_EXPR = begin(__range)
11718 END_EXPR = end(__range);
11719
11720 If __range has a member 'begin' but not 'end', or vice versa, we must
11721 still use the second alternative (it will surely fail, however).
11722 When calling begin()/end() in the third alternative we must use
11723 argument dependent lookup, but always considering 'std' as an associated
11724 namespace. */
11725
11726tree
11727cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11728 tree decomp_first_name, unsigned int decomp_cnt,
11729 bool ivdep)
11730{
11731 tree begin, end;
11732 tree iter_type, begin_expr, end_expr;
11733 tree condition, expression;
11734
11735 if (range_decl == error_mark_node || range_expr == error_mark_node)
11736 /* If an error happened previously do nothing or else a lot of
11737 unhelpful errors would be issued. */
11738 begin_expr = end_expr = iter_type = error_mark_node;
11739 else
11740 {
11741 tree range_temp;
11742
11743 if (VAR_P (range_expr)
11744 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11745 /* Can't bind a reference to an array of runtime bound. */
11746 range_temp = range_expr;
11747 else
11748 {
11749 range_temp = build_range_temp (range_expr);
11750 pushdecl (range_temp);
11751 cp_finish_decl (range_temp, range_expr,
11752 /*is_constant_init*/false, NULL_TREE,
11753 LOOKUP_ONLYCONVERTING);
11754 range_temp = convert_from_reference (range_temp);
11755 }
11756 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11757 &begin_expr, &end_expr);
11758 }
11759
11760 /* The new for initialization statement. */
11761 begin = build_decl (input_location, VAR_DECL,
11762 get_identifier ("__for_begin"), iter_type);
11763 TREE_USED (begin) = 1;
11764 DECL_ARTIFICIAL (begin) = 1;
11765 pushdecl (begin);
11766 cp_finish_decl (begin, begin_expr,
11767 /*is_constant_init*/false, NULL_TREE,
11768 LOOKUP_ONLYCONVERTING);
11769
11770 if (cxx_dialect >= cxx1z)
11771 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11772 end = build_decl (input_location, VAR_DECL,
11773 get_identifier ("__for_end"), iter_type);
11774 TREE_USED (end) = 1;
11775 DECL_ARTIFICIAL (end) = 1;
11776 pushdecl (end);
11777 cp_finish_decl (end, end_expr,
11778 /*is_constant_init*/false, NULL_TREE,
11779 LOOKUP_ONLYCONVERTING);
11780
11781 finish_init_stmt (statement);
11782
11783 /* The new for condition. */
11784 condition = build_x_binary_op (input_location, NE_EXPR,
11785 begin, ERROR_MARK,
11786 end, ERROR_MARK,
11787 NULL, tf_warning_or_error);
11788 finish_for_cond (condition, statement, ivdep);
11789
11790 /* The new increment expression. */
11791 expression = finish_unary_op_expr (input_location,
11792 PREINCREMENT_EXPR, begin,
11793 tf_warning_or_error);
11794 finish_for_expr (expression, statement);
11795
11796 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11797 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
11798
11799 /* The declaration is initialized with *__begin inside the loop body. */
11800 cp_finish_decl (range_decl,
11801 build_x_indirect_ref (input_location, begin, RO_NULL,
11802 tf_warning_or_error),
11803 /*is_constant_init*/false, NULL_TREE,
11804 LOOKUP_ONLYCONVERTING);
11805 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11806 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11807
11808 return statement;
11809}
11810
11811/* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11812 We need to solve both at the same time because the method used
11813 depends on the existence of members begin or end.
11814 Returns the type deduced for the iterator expression. */
11815
11816static tree
11817cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11818{
11819 if (error_operand_p (range))
11820 {
11821 *begin = *end = error_mark_node;
11822 return error_mark_node;
11823 }
11824
11825 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11826 {
11827 error ("range-based %<for%> expression of type %qT "
11828 "has incomplete type", TREE_TYPE (range));
11829 *begin = *end = error_mark_node;
11830 return error_mark_node;
11831 }
11832 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11833 {
11834 /* If RANGE is an array, we will use pointer arithmetic. */
11835 *begin = decay_conversion (range, tf_warning_or_error);
11836 *end = build_binary_op (input_location, PLUS_EXPR,
11837 range,
11838 array_type_nelts_top (TREE_TYPE (range)),
11839 0);
11840 return TREE_TYPE (*begin);
11841 }
11842 else
11843 {
11844 /* If it is not an array, we must do a bit of magic. */
11845 tree id_begin, id_end;
11846 tree member_begin, member_end;
11847
11848 *begin = *end = error_mark_node;
11849
11850 id_begin = get_identifier ("begin");
11851 id_end = get_identifier ("end");
11852 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11853 /*protect=*/2, /*want_type=*/false,
11854 tf_warning_or_error);
11855 member_end = lookup_member (TREE_TYPE (range), id_end,
11856 /*protect=*/2, /*want_type=*/false,
11857 tf_warning_or_error);
11858
11859 if (member_begin != NULL_TREE && member_end != NULL_TREE)
11860 {
11861 /* Use the member functions. */
11862 if (member_begin != NULL_TREE)
11863 *begin = cp_parser_range_for_member_function (range, id_begin);
11864 else
11865 error ("range-based %<for%> expression of type %qT has an "
11866 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11867
11868 if (member_end != NULL_TREE)
11869 *end = cp_parser_range_for_member_function (range, id_end);
11870 else
11871 error ("range-based %<for%> expression of type %qT has a "
11872 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11873 }
11874 else
11875 {
11876 /* Use global functions with ADL. */
11877 vec<tree, va_gc> *vec;
11878 vec = make_tree_vector ();
11879
11880 vec_safe_push (vec, range);
11881
11882 member_begin = perform_koenig_lookup (id_begin, vec,
11883 tf_warning_or_error);
11884 *begin = finish_call_expr (member_begin, &vec, false, true,
11885 tf_warning_or_error);
11886 member_end = perform_koenig_lookup (id_end, vec,
11887 tf_warning_or_error);
11888 *end = finish_call_expr (member_end, &vec, false, true,
11889 tf_warning_or_error);
11890
11891 release_tree_vector (vec);
11892 }
11893
11894 /* Last common checks. */
11895 if (*begin == error_mark_node || *end == error_mark_node)
11896 {
11897 /* If one of the expressions is an error do no more checks. */
11898 *begin = *end = error_mark_node;
11899 return error_mark_node;
11900 }
11901 else if (type_dependent_expression_p (*begin)
11902 || type_dependent_expression_p (*end))
11903 /* Can happen, when, eg, in a template context, Koenig lookup
11904 can't resolve begin/end (c++/58503). */
11905 return NULL_TREE;
11906 else
11907 {
11908 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11909 /* The unqualified type of the __begin and __end temporaries should
11910 be the same, as required by the multiple auto declaration. */
11911 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11912 {
11913 if (cxx_dialect >= cxx1z
11914 && (build_x_binary_op (input_location, NE_EXPR,
11915 *begin, ERROR_MARK,
11916 *end, ERROR_MARK,
11917 NULL, tf_none)
11918 != error_mark_node))
11919 /* P0184R0 allows __begin and __end to have different types,
11920 but make sure they are comparable so we can give a better
11921 diagnostic. */;
11922 else
11923 error ("inconsistent begin/end types in range-based %<for%> "
11924 "statement: %qT and %qT",
11925 TREE_TYPE (*begin), TREE_TYPE (*end));
11926 }
11927 return iter_type;
11928 }
11929 }
11930}
11931
11932/* Helper function for cp_parser_perform_range_for_lookup.
11933 Builds a tree for RANGE.IDENTIFIER(). */
11934
11935static tree
11936cp_parser_range_for_member_function (tree range, tree identifier)
11937{
11938 tree member, res;
11939 vec<tree, va_gc> *vec;
11940
11941 member = finish_class_member_access_expr (range, identifier,
11942 false, tf_warning_or_error);
11943 if (member == error_mark_node)
11944 return error_mark_node;
11945
11946 vec = make_tree_vector ();
11947 res = finish_call_expr (member, &vec,
11948 /*disallow_virtual=*/false,
11949 /*koenig_p=*/false,
11950 tf_warning_or_error);
11951 release_tree_vector (vec);
11952 return res;
11953}
11954
11955/* Parse an iteration-statement.
11956
11957 iteration-statement:
11958 while ( condition ) statement
11959 do statement while ( expression ) ;
11960 for ( init-statement condition [opt] ; expression [opt] )
11961 statement
11962
11963 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11964
11965static tree
11966cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11967{
11968 cp_token *token;
11969 enum rid keyword;
11970 tree statement;
11971 unsigned char in_statement;
11972 token_indent_info guard_tinfo;
11973
11974 /* Peek at the next token. */
11975 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11976 if (!token)
11977 return error_mark_node;
11978
11979 guard_tinfo = get_token_indent_info (token);
11980
11981 /* Remember whether or not we are already within an iteration
11982 statement. */
11983 in_statement = parser->in_statement;
11984
11985 /* See what kind of keyword it is. */
11986 keyword = token->keyword;
11987 switch (keyword)
11988 {
11989 case RID_WHILE:
11990 {
11991 tree condition;
11992
11993 /* Begin the while-statement. */
11994 statement = begin_while_stmt ();
11995 /* Look for the `('. */
11996 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11997 /* Parse the condition. */
11998 condition = cp_parser_condition (parser);
11999 finish_while_stmt_cond (condition, statement, ivdep);
12000 /* Look for the `)'. */
12001 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12002 /* Parse the dependent statement. */
12003 parser->in_statement = IN_ITERATION_STMT;
12004 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12005 parser->in_statement = in_statement;
12006 /* We're done with the while-statement. */
12007 finish_while_stmt (statement);
12008 }
12009 break;
12010
12011 case RID_DO:
12012 {
12013 tree expression;
12014
12015 /* Begin the do-statement. */
12016 statement = begin_do_stmt ();
12017 /* Parse the body of the do-statement. */
12018 parser->in_statement = IN_ITERATION_STMT;
12019 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12020 parser->in_statement = in_statement;
12021 finish_do_body (statement);
12022 /* Look for the `while' keyword. */
12023 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12024 /* Look for the `('. */
12025 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12026 /* Parse the expression. */
12027 expression = cp_parser_expression (parser);
12028 /* We're done with the do-statement. */
12029 finish_do_stmt (expression, statement, ivdep);
12030 /* Look for the `)'. */
12031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12032 /* Look for the `;'. */
12033 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12034 }
12035 break;
12036
12037 case RID_FOR:
12038 {
12039 /* Look for the `('. */
12040 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12041
12042 statement = cp_parser_for (parser, ivdep);
12043
12044 /* Look for the `)'. */
12045 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12046
12047 /* Parse the body of the for-statement. */
12048 parser->in_statement = IN_ITERATION_STMT;
12049 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12050 parser->in_statement = in_statement;
12051
12052 /* We're done with the for-statement. */
12053 finish_for_stmt (statement);
12054 }
12055 break;
12056
12057 default:
12058 cp_parser_error (parser, "expected iteration-statement");
12059 statement = error_mark_node;
12060 break;
12061 }
12062
12063 return statement;
12064}
12065
12066/* Parse a init-statement or the declarator of a range-based-for.
12067 Returns true if a range-based-for declaration is seen.
12068
12069 init-statement:
12070 expression-statement
12071 simple-declaration */
12072
12073static bool
12074cp_parser_init_statement (cp_parser* parser, tree *decl)
12075{
12076 /* If the next token is a `;', then we have an empty
12077 expression-statement. Grammatically, this is also a
12078 simple-declaration, but an invalid one, because it does not
12079 declare anything. Therefore, if we did not handle this case
12080 specially, we would issue an error message about an invalid
12081 declaration. */
12082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12083 {
12084 bool is_range_for = false;
12085 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12086
12087 /* A colon is used in range-based for. */
12088 parser->colon_corrects_to_scope_p = false;
12089
12090 /* We're going to speculatively look for a declaration, falling back
12091 to an expression, if necessary. */
12092 cp_parser_parse_tentatively (parser);
12093 /* Parse the declaration. */
12094 cp_parser_simple_declaration (parser,
12095 /*function_definition_allowed_p=*/false,
12096 decl);
12097 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12098 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12099 {
12100 /* It is a range-for, consume the ':' */
12101 cp_lexer_consume_token (parser->lexer);
12102 is_range_for = true;
12103 if (cxx_dialect < cxx11)
12104 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12105 "range-based %<for%> loops only available with "
12106 "-std=c++11 or -std=gnu++11");
12107 }
12108 else
12109 /* The ';' is not consumed yet because we told
12110 cp_parser_simple_declaration not to. */
12111 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12112
12113 if (cp_parser_parse_definitely (parser))
12114 return is_range_for;
12115 /* If the tentative parse failed, then we shall need to look for an
12116 expression-statement. */
12117 }
12118 /* If we are here, it is an expression-statement. */
12119 cp_parser_expression_statement (parser, NULL_TREE);
12120 return false;
12121}
12122
12123/* Parse a jump-statement.
12124
12125 jump-statement:
12126 break ;
12127 continue ;
12128 return expression [opt] ;
12129 return braced-init-list ;
12130 goto identifier ;
12131
12132 GNU extension:
12133
12134 jump-statement:
12135 goto * expression ;
12136
12137 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12138
12139static tree
12140cp_parser_jump_statement (cp_parser* parser)
12141{
12142 tree statement = error_mark_node;
12143 cp_token *token;
12144 enum rid keyword;
12145 unsigned char in_statement;
12146
12147 /* Peek at the next token. */
12148 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12149 if (!token)
12150 return error_mark_node;
12151
12152 /* See what kind of keyword it is. */
12153 keyword = token->keyword;
12154 switch (keyword)
12155 {
12156 case RID_BREAK:
12157 in_statement = parser->in_statement & ~IN_IF_STMT;
12158 switch (in_statement)
12159 {
12160 case 0:
12161 error_at (token->location, "break statement not within loop or switch");
12162 break;
12163 default:
12164 gcc_assert ((in_statement & IN_SWITCH_STMT)
12165 || in_statement == IN_ITERATION_STMT);
12166 statement = finish_break_stmt ();
12167 if (in_statement == IN_ITERATION_STMT)
12168 break_maybe_infinite_loop ();
12169 break;
12170 case IN_OMP_BLOCK:
12171 error_at (token->location, "invalid exit from OpenMP structured block");
12172 break;
12173 case IN_OMP_FOR:
12174 error_at (token->location, "break statement used with OpenMP for loop");
12175 break;
12176 case IN_CILK_SIMD_FOR:
12177 error_at (token->location, "break statement used with Cilk Plus for loop");
12178 break;
12179 }
12180 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12181 break;
12182
12183 case RID_CONTINUE:
12184 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12185 {
12186 case 0:
12187 error_at (token->location, "continue statement not within a loop");
12188 break;
12189 case IN_CILK_SIMD_FOR:
12190 error_at (token->location,
12191 "continue statement within %<#pragma simd%> loop body");
12192 /* Fall through. */
12193 case IN_ITERATION_STMT:
12194 case IN_OMP_FOR:
12195 statement = finish_continue_stmt ();
12196 break;
12197 case IN_OMP_BLOCK:
12198 error_at (token->location, "invalid exit from OpenMP structured block");
12199 break;
12200 default:
12201 gcc_unreachable ();
12202 }
12203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12204 break;
12205
12206 case RID_RETURN:
12207 {
12208 tree expr;
12209 bool expr_non_constant_p;
12210
12211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12212 {
12213 cp_lexer_set_source_position (parser->lexer);
12214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12215 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12216 }
12217 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12218 expr = cp_parser_expression (parser);
12219 else
12220 /* If the next token is a `;', then there is no
12221 expression. */
12222 expr = NULL_TREE;
12223 /* Build the return-statement. */
12224 if (current_function_auto_return_pattern && in_discarded_stmt)
12225 /* Don't deduce from a discarded return statement. */;
12226 else
12227 statement = finish_return_stmt (expr);
12228 /* Look for the final `;'. */
12229 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12230 }
12231 break;
12232
12233 case RID_GOTO:
12234 if (parser->in_function_body
12235 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12236 {
12237 error ("%<goto%> in %<constexpr%> function");
12238 cp_function_chain->invalid_constexpr = true;
12239 }
12240
12241 /* Create the goto-statement. */
12242 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12243 {
12244 /* Issue a warning about this use of a GNU extension. */
12245 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12246 /* Consume the '*' token. */
12247 cp_lexer_consume_token (parser->lexer);
12248 /* Parse the dependent expression. */
12249 finish_goto_stmt (cp_parser_expression (parser));
12250 }
12251 else
12252 finish_goto_stmt (cp_parser_identifier (parser));
12253 /* Look for the final `;'. */
12254 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12255 break;
12256
12257 default:
12258 cp_parser_error (parser, "expected jump-statement");
12259 break;
12260 }
12261
12262 return statement;
12263}
12264
12265/* Parse a declaration-statement.
12266
12267 declaration-statement:
12268 block-declaration */
12269
12270static void
12271cp_parser_declaration_statement (cp_parser* parser)
12272{
12273 void *p;
12274
12275 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12276 p = obstack_alloc (&declarator_obstack, 0);
12277
12278 /* Parse the block-declaration. */
12279 cp_parser_block_declaration (parser, /*statement_p=*/true);
12280
12281 /* Free any declarators allocated. */
12282 obstack_free (&declarator_obstack, p);
12283}
12284
12285/* Some dependent statements (like `if (cond) statement'), are
12286 implicitly in their own scope. In other words, if the statement is
12287 a single statement (as opposed to a compound-statement), it is
12288 none-the-less treated as if it were enclosed in braces. Any
12289 declarations appearing in the dependent statement are out of scope
12290 after control passes that point. This function parses a statement,
12291 but ensures that is in its own scope, even if it is not a
12292 compound-statement.
12293
12294 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12295 is a (possibly labeled) if statement which is not enclosed in
12296 braces and has an else clause. This is used to implement
12297 -Wparentheses.
12298
12299 CHAIN is a vector of if-else-if conditions. This is used to implement
12300 -Wduplicated-cond.
12301
12302 Returns the new statement. */
12303
12304static tree
12305cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12306 const token_indent_info &guard_tinfo,
12307 vec<tree> *chain)
12308{
12309 tree statement;
12310 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12311 token_indent_info body_tinfo
12312 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12313
12314 if (if_p != NULL)
12315 *if_p = false;
12316
12317 /* Mark if () ; with a special NOP_EXPR. */
12318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12319 {
12320 cp_lexer_consume_token (parser->lexer);
12321 statement = add_stmt (build_empty_stmt (body_loc));
12322
12323 if (guard_tinfo.keyword == RID_IF
12324 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12325 warning_at (body_loc, OPT_Wempty_body,
12326 "suggest braces around empty body in an %<if%> statement");
12327 else if (guard_tinfo.keyword == RID_ELSE)
12328 warning_at (body_loc, OPT_Wempty_body,
12329 "suggest braces around empty body in an %<else%> statement");
12330 }
12331 /* if a compound is opened, we simply parse the statement directly. */
12332 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12333 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12334 /* If the token is not a `{', then we must take special action. */
12335 else
12336 {
12337 /* Create a compound-statement. */
12338 statement = begin_compound_stmt (0);
12339 /* Parse the dependent-statement. */
12340 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12341 /* Finish the dummy compound-statement. */
12342 finish_compound_stmt (statement);
12343 }
12344
12345 token_indent_info next_tinfo
12346 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12347 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12348
12349 /* Return the statement. */
12350 return statement;
12351}
12352
12353/* For some dependent statements (like `while (cond) statement'), we
12354 have already created a scope. Therefore, even if the dependent
12355 statement is a compound-statement, we do not want to create another
12356 scope. */
12357
12358static void
12359cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12360 const token_indent_info &guard_tinfo)
12361{
12362 /* If the token is a `{', then we must take special action. */
12363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12364 {
12365 token_indent_info body_tinfo
12366 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12367
12368 cp_parser_statement (parser, NULL_TREE, false, if_p);
12369 token_indent_info next_tinfo
12370 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12371 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12372 }
12373 else
12374 {
12375 /* Avoid calling cp_parser_compound_statement, so that we
12376 don't create a new scope. Do everything else by hand. */
12377 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12378 /* If the next keyword is `__label__' we have a label declaration. */
12379 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12380 cp_parser_label_declaration (parser);
12381 /* Parse an (optional) statement-seq. */
12382 cp_parser_statement_seq_opt (parser, NULL_TREE);
12383 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12384 }
12385}
12386
12387/* Declarations [gram.dcl.dcl] */
12388
12389/* Parse an optional declaration-sequence.
12390
12391 declaration-seq:
12392 declaration
12393 declaration-seq declaration */
12394
12395static void
12396cp_parser_declaration_seq_opt (cp_parser* parser)
12397{
12398 while (true)
12399 {
12400 cp_token *token;
12401
12402 token = cp_lexer_peek_token (parser->lexer);
12403
12404 if (token->type == CPP_CLOSE_BRACE
12405 || token->type == CPP_EOF
12406 || token->type == CPP_PRAGMA_EOL)
12407 break;
12408
12409 if (token->type == CPP_SEMICOLON)
12410 {
12411 /* A declaration consisting of a single semicolon is
12412 invalid. Allow it unless we're being pedantic. */
12413 cp_lexer_consume_token (parser->lexer);
12414 if (!in_system_header_at (input_location))
12415 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12416 continue;
12417 }
12418
12419 /* If we're entering or exiting a region that's implicitly
12420 extern "C", modify the lang context appropriately. */
12421 if (!parser->implicit_extern_c && token->implicit_extern_c)
12422 {
12423 push_lang_context (lang_name_c);
12424 parser->implicit_extern_c = true;
12425 }
12426 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12427 {
12428 pop_lang_context ();
12429 parser->implicit_extern_c = false;
12430 }
12431
12432 if (token->type == CPP_PRAGMA)
12433 {
12434 /* A top-level declaration can consist solely of a #pragma.
12435 A nested declaration cannot, so this is done here and not
12436 in cp_parser_declaration. (A #pragma at block scope is
12437 handled in cp_parser_statement.) */
12438 cp_parser_pragma (parser, pragma_external, NULL);
12439 continue;
12440 }
12441
12442 /* Parse the declaration itself. */
12443 cp_parser_declaration (parser);
12444 }
12445}
12446
12447/* Parse a declaration.
12448
12449 declaration:
12450 block-declaration
12451 function-definition
12452 template-declaration
12453 explicit-instantiation
12454 explicit-specialization
12455 linkage-specification
12456 namespace-definition
12457
12458 C++17:
12459 deduction-guide
12460
12461 GNU extension:
12462
12463 declaration:
12464 __extension__ declaration */
12465
12466static void
12467cp_parser_declaration (cp_parser* parser)
12468{
12469 cp_token token1;
12470 cp_token token2;
12471 int saved_pedantic;
12472 void *p;
12473 tree attributes = NULL_TREE;
12474
12475 /* Check for the `__extension__' keyword. */
12476 if (cp_parser_extension_opt (parser, &saved_pedantic))
12477 {
12478 /* Parse the qualified declaration. */
12479 cp_parser_declaration (parser);
12480 /* Restore the PEDANTIC flag. */
12481 pedantic = saved_pedantic;
12482
12483 return;
12484 }
12485
12486 /* Try to figure out what kind of declaration is present. */
12487 token1 = *cp_lexer_peek_token (parser->lexer);
12488
12489 if (token1.type != CPP_EOF)
12490 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12491 else
12492 {
12493 token2.type = CPP_EOF;
12494 token2.keyword = RID_MAX;
12495 }
12496
12497 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12498 p = obstack_alloc (&declarator_obstack, 0);
12499
12500 /* If the next token is `extern' and the following token is a string
12501 literal, then we have a linkage specification. */
12502 if (token1.keyword == RID_EXTERN
12503 && cp_parser_is_pure_string_literal (&token2))
12504 cp_parser_linkage_specification (parser);
12505 /* If the next token is `template', then we have either a template
12506 declaration, an explicit instantiation, or an explicit
12507 specialization. */
12508 else if (token1.keyword == RID_TEMPLATE)
12509 {
12510 /* `template <>' indicates a template specialization. */
12511 if (token2.type == CPP_LESS
12512 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12513 cp_parser_explicit_specialization (parser);
12514 /* `template <' indicates a template declaration. */
12515 else if (token2.type == CPP_LESS)
12516 cp_parser_template_declaration (parser, /*member_p=*/false);
12517 /* Anything else must be an explicit instantiation. */
12518 else
12519 cp_parser_explicit_instantiation (parser);
12520 }
12521 /* If the next token is `export', then we have a template
12522 declaration. */
12523 else if (token1.keyword == RID_EXPORT)
12524 cp_parser_template_declaration (parser, /*member_p=*/false);
12525 /* If the next token is `extern', 'static' or 'inline' and the one
12526 after that is `template', we have a GNU extended explicit
12527 instantiation directive. */
12528 else if (cp_parser_allow_gnu_extensions_p (parser)
12529 && (token1.keyword == RID_EXTERN
12530 || token1.keyword == RID_STATIC
12531 || token1.keyword == RID_INLINE)
12532 && token2.keyword == RID_TEMPLATE)
12533 cp_parser_explicit_instantiation (parser);
12534 /* If the next token is `namespace', check for a named or unnamed
12535 namespace definition. */
12536 else if (token1.keyword == RID_NAMESPACE
12537 && (/* A named namespace definition. */
12538 (token2.type == CPP_NAME
12539 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12540 != CPP_EQ))
12541 || (token2.type == CPP_OPEN_SQUARE
12542 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12543 == CPP_OPEN_SQUARE)
12544 /* An unnamed namespace definition. */
12545 || token2.type == CPP_OPEN_BRACE
12546 || token2.keyword == RID_ATTRIBUTE))
12547 cp_parser_namespace_definition (parser);
12548 /* An inline (associated) namespace definition. */
12549 else if (token1.keyword == RID_INLINE
12550 && token2.keyword == RID_NAMESPACE)
12551 cp_parser_namespace_definition (parser);
12552 /* Objective-C++ declaration/definition. */
12553 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12554 cp_parser_objc_declaration (parser, NULL_TREE);
12555 else if (c_dialect_objc ()
12556 && token1.keyword == RID_ATTRIBUTE
12557 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12558 cp_parser_objc_declaration (parser, attributes);
12559 /* At this point we may have a template declared by a concept
12560 introduction. */
12561 else if (flag_concepts
12562 && cp_parser_template_declaration_after_export (parser,
12563 /*member_p=*/false))
12564 /* We did. */;
12565 else
12566 /* Try to parse a block-declaration, or a function-definition. */
12567 cp_parser_block_declaration (parser, /*statement_p=*/false);
12568
12569 /* Free any declarators allocated. */
12570 obstack_free (&declarator_obstack, p);
12571}
12572
12573/* Parse a block-declaration.
12574
12575 block-declaration:
12576 simple-declaration
12577 asm-definition
12578 namespace-alias-definition
12579 using-declaration
12580 using-directive
12581
12582 GNU Extension:
12583
12584 block-declaration:
12585 __extension__ block-declaration
12586
12587 C++0x Extension:
12588
12589 block-declaration:
12590 static_assert-declaration
12591
12592 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12593 part of a declaration-statement. */
12594
12595static void
12596cp_parser_block_declaration (cp_parser *parser,
12597 bool statement_p)
12598{
12599 cp_token *token1;
12600 int saved_pedantic;
12601
12602 /* Check for the `__extension__' keyword. */
12603 if (cp_parser_extension_opt (parser, &saved_pedantic))
12604 {
12605 /* Parse the qualified declaration. */
12606 cp_parser_block_declaration (parser, statement_p);
12607 /* Restore the PEDANTIC flag. */
12608 pedantic = saved_pedantic;
12609
12610 return;
12611 }
12612
12613 /* Peek at the next token to figure out which kind of declaration is
12614 present. */
12615 token1 = cp_lexer_peek_token (parser->lexer);
12616
12617 /* If the next keyword is `asm', we have an asm-definition. */
12618 if (token1->keyword == RID_ASM)
12619 {
12620 if (statement_p)
12621 cp_parser_commit_to_tentative_parse (parser);
12622 cp_parser_asm_definition (parser);
12623 }
12624 /* If the next keyword is `namespace', we have a
12625 namespace-alias-definition. */
12626 else if (token1->keyword == RID_NAMESPACE)
12627 cp_parser_namespace_alias_definition (parser);
12628 /* If the next keyword is `using', we have a
12629 using-declaration, a using-directive, or an alias-declaration. */
12630 else if (token1->keyword == RID_USING)
12631 {
12632 cp_token *token2;
12633
12634 if (statement_p)
12635 cp_parser_commit_to_tentative_parse (parser);
12636 /* If the token after `using' is `namespace', then we have a
12637 using-directive. */
12638 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12639 if (token2->keyword == RID_NAMESPACE)
12640 cp_parser_using_directive (parser);
12641 /* If the second token after 'using' is '=', then we have an
12642 alias-declaration. */
12643 else if (cxx_dialect >= cxx11
12644 && token2->type == CPP_NAME
12645 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12646 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12647 cp_parser_alias_declaration (parser);
12648 /* Otherwise, it's a using-declaration. */
12649 else
12650 cp_parser_using_declaration (parser,
12651 /*access_declaration_p=*/false);
12652 }
12653 /* If the next keyword is `__label__' we have a misplaced label
12654 declaration. */
12655 else if (token1->keyword == RID_LABEL)
12656 {
12657 cp_lexer_consume_token (parser->lexer);
12658 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12659 cp_parser_skip_to_end_of_statement (parser);
12660 /* If the next token is now a `;', consume it. */
12661 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12662 cp_lexer_consume_token (parser->lexer);
12663 }
12664 /* If the next token is `static_assert' we have a static assertion. */
12665 else if (token1->keyword == RID_STATIC_ASSERT)
12666 cp_parser_static_assert (parser, /*member_p=*/false);
12667 /* Anything else must be a simple-declaration. */
12668 else
12669 cp_parser_simple_declaration (parser, !statement_p,
12670 /*maybe_range_for_decl*/NULL);
12671}
12672
12673/* Parse a simple-declaration.
12674
12675 simple-declaration:
12676 decl-specifier-seq [opt] init-declarator-list [opt] ;
12677 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12678 brace-or-equal-initializer ;
12679
12680 init-declarator-list:
12681 init-declarator
12682 init-declarator-list , init-declarator
12683
12684 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12685 function-definition as a simple-declaration.
12686
12687 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12688 parsed declaration if it is an uninitialized single declarator not followed
12689 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12690 if present, will not be consumed. */
12691
12692static void
12693cp_parser_simple_declaration (cp_parser* parser,
12694 bool function_definition_allowed_p,
12695 tree *maybe_range_for_decl)
12696{
12697 cp_decl_specifier_seq decl_specifiers;
12698 int declares_class_or_enum;
12699 bool saw_declarator;
12700 location_t comma_loc = UNKNOWN_LOCATION;
12701 location_t init_loc = UNKNOWN_LOCATION;
12702
12703 if (maybe_range_for_decl)
12704 *maybe_range_for_decl = NULL_TREE;
12705
12706 /* Defer access checks until we know what is being declared; the
12707 checks for names appearing in the decl-specifier-seq should be
12708 done as if we were in the scope of the thing being declared. */
12709 push_deferring_access_checks (dk_deferred);
12710
12711 /* Parse the decl-specifier-seq. We have to keep track of whether
12712 or not the decl-specifier-seq declares a named class or
12713 enumeration type, since that is the only case in which the
12714 init-declarator-list is allowed to be empty.
12715
12716 [dcl.dcl]
12717
12718 In a simple-declaration, the optional init-declarator-list can be
12719 omitted only when declaring a class or enumeration, that is when
12720 the decl-specifier-seq contains either a class-specifier, an
12721 elaborated-type-specifier, or an enum-specifier. */
12722 cp_parser_decl_specifier_seq (parser,
12723 CP_PARSER_FLAGS_OPTIONAL,
12724 &decl_specifiers,
12725 &declares_class_or_enum);
12726 /* We no longer need to defer access checks. */
12727 stop_deferring_access_checks ();
12728
12729 /* In a block scope, a valid declaration must always have a
12730 decl-specifier-seq. By not trying to parse declarators, we can
12731 resolve the declaration/expression ambiguity more quickly. */
12732 if (!function_definition_allowed_p
12733 && !decl_specifiers.any_specifiers_p)
12734 {
12735 cp_parser_error (parser, "expected declaration");
12736 goto done;
12737 }
12738
12739 /* If the next two tokens are both identifiers, the code is
12740 erroneous. The usual cause of this situation is code like:
12741
12742 T t;
12743
12744 where "T" should name a type -- but does not. */
12745 if (!decl_specifiers.any_type_specifiers_p
12746 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12747 {
12748 /* If parsing tentatively, we should commit; we really are
12749 looking at a declaration. */
12750 cp_parser_commit_to_tentative_parse (parser);
12751 /* Give up. */
12752 goto done;
12753 }
12754
12755 /* If we have seen at least one decl-specifier, and the next token
12756 is not a parenthesis, then we must be looking at a declaration.
12757 (After "int (" we might be looking at a functional cast.) */
12758 if (decl_specifiers.any_specifiers_p
12759 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12760 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12761 && !cp_parser_error_occurred (parser))
12762 cp_parser_commit_to_tentative_parse (parser);
12763
12764 /* Look for C++17 decomposition declaration. */
12765 for (size_t n = 1; ; n++)
12766 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12767 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12768 continue;
12769 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12770 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12771 && decl_specifiers.any_specifiers_p)
12772 {
12773 tree decl
12774 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12775 maybe_range_for_decl,
12776 &init_loc);
12777
12778 /* The next token should be either a `,' or a `;'. */
12779 cp_token *token = cp_lexer_peek_token (parser->lexer);
12780 /* If it's a `;', we are done. */
12781 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12782 goto finish;
12783 /* Anything else is an error. */
12784 else
12785 {
12786 /* If we have already issued an error message we don't need
12787 to issue another one. */
12788 if ((decl != error_mark_node
12789 && DECL_INITIAL (decl) != error_mark_node)
12790 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12791 cp_parser_error (parser, "expected %<,%> or %<;%>");
12792 /* Skip tokens until we reach the end of the statement. */
12793 cp_parser_skip_to_end_of_statement (parser);
12794 /* If the next token is now a `;', consume it. */
12795 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12796 cp_lexer_consume_token (parser->lexer);
12797 goto done;
12798 }
12799 }
12800 else
12801 break;
12802
12803 tree last_type;
12804 bool auto_specifier_p;
12805 /* NULL_TREE if both variable and function declaration are allowed,
12806 error_mark_node if function declaration are not allowed and
12807 a FUNCTION_DECL that should be diagnosed if it is followed by
12808 variable declarations. */
12809 tree auto_function_declaration;
12810
12811 last_type = NULL_TREE;
12812 auto_specifier_p
12813 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12814 auto_function_declaration = NULL_TREE;
12815
12816 /* Keep going until we hit the `;' at the end of the simple
12817 declaration. */
12818 saw_declarator = false;
12819 while (cp_lexer_next_token_is_not (parser->lexer,
12820 CPP_SEMICOLON))
12821 {
12822 cp_token *token;
12823 bool function_definition_p;
12824 tree decl;
12825 tree auto_result = NULL_TREE;
12826
12827 if (saw_declarator)
12828 {
12829 /* If we are processing next declarator, comma is expected */
12830 token = cp_lexer_peek_token (parser->lexer);
12831 gcc_assert (token->type == CPP_COMMA);
12832 cp_lexer_consume_token (parser->lexer);
12833 if (maybe_range_for_decl)
12834 {
12835 *maybe_range_for_decl = error_mark_node;
12836 if (comma_loc == UNKNOWN_LOCATION)
12837 comma_loc = token->location;
12838 }
12839 }
12840 else
12841 saw_declarator = true;
12842
12843 /* Parse the init-declarator. */
12844 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12845 /*checks=*/NULL,
12846 function_definition_allowed_p,
12847 /*member_p=*/false,
12848 declares_class_or_enum,
12849 &function_definition_p,
12850 maybe_range_for_decl,
12851 &init_loc,
12852 &auto_result);
12853 /* If an error occurred while parsing tentatively, exit quickly.
12854 (That usually happens when in the body of a function; each
12855 statement is treated as a declaration-statement until proven
12856 otherwise.) */
12857 if (cp_parser_error_occurred (parser))
12858 goto done;
12859
12860 if (auto_specifier_p && cxx_dialect >= cxx14)
12861 {
12862 /* If the init-declarator-list contains more than one
12863 init-declarator, they shall all form declarations of
12864 variables. */
12865 if (auto_function_declaration == NULL_TREE)
12866 auto_function_declaration
12867 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12868 else if (TREE_CODE (decl) == FUNCTION_DECL
12869 || auto_function_declaration != error_mark_node)
12870 {
12871 error_at (decl_specifiers.locations[ds_type_spec],
12872 "non-variable %qD in declaration with more than one "
12873 "declarator with placeholder type",
12874 TREE_CODE (decl) == FUNCTION_DECL
12875 ? decl : auto_function_declaration);
12876 auto_function_declaration = error_mark_node;
12877 }
12878 }
12879
12880 if (auto_result
12881 && (!processing_template_decl || !type_uses_auto (auto_result)))
12882 {
12883 if (last_type
12884 && last_type != error_mark_node
12885 && !same_type_p (auto_result, last_type))
12886 {
12887 /* If the list of declarators contains more than one declarator,
12888 the type of each declared variable is determined as described
12889 above. If the type deduced for the template parameter U is not
12890 the same in each deduction, the program is ill-formed. */
12891 error_at (decl_specifiers.locations[ds_type_spec],
12892 "inconsistent deduction for %qT: %qT and then %qT",
12893 decl_specifiers.type, last_type, auto_result);
12894 last_type = error_mark_node;
12895 }
12896 else
12897 last_type = auto_result;
12898 }
12899
12900 /* Handle function definitions specially. */
12901 if (function_definition_p)
12902 {
12903 /* If the next token is a `,', then we are probably
12904 processing something like:
12905
12906 void f() {}, *p;
12907
12908 which is erroneous. */
12909 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12910 {
12911 cp_token *token = cp_lexer_peek_token (parser->lexer);
12912 error_at (token->location,
12913 "mixing"
12914 " declarations and function-definitions is forbidden");
12915 }
12916 /* Otherwise, we're done with the list of declarators. */
12917 else
12918 {
12919 pop_deferring_access_checks ();
12920 return;
12921 }
12922 }
12923 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12924 *maybe_range_for_decl = decl;
12925 /* The next token should be either a `,' or a `;'. */
12926 token = cp_lexer_peek_token (parser->lexer);
12927 /* If it's a `,', there are more declarators to come. */
12928 if (token->type == CPP_COMMA)
12929 /* will be consumed next time around */;
12930 /* If it's a `;', we are done. */
12931 else if (token->type == CPP_SEMICOLON)
12932 break;
12933 else if (maybe_range_for_decl)
12934 {
12935 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12936 permerror (decl_specifiers.locations[ds_type_spec],
12937 "types may not be defined in a for-range-declaration");
12938 break;
12939 }
12940 /* Anything else is an error. */
12941 else
12942 {
12943 /* If we have already issued an error message we don't need
12944 to issue another one. */
12945 if ((decl != error_mark_node
12946 && DECL_INITIAL (decl) != error_mark_node)
12947 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12948 cp_parser_error (parser, "expected %<,%> or %<;%>");
12949 /* Skip tokens until we reach the end of the statement. */
12950 cp_parser_skip_to_end_of_statement (parser);
12951 /* If the next token is now a `;', consume it. */
12952 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12953 cp_lexer_consume_token (parser->lexer);
12954 goto done;
12955 }
12956 /* After the first time around, a function-definition is not
12957 allowed -- even if it was OK at first. For example:
12958
12959 int i, f() {}
12960
12961 is not valid. */
12962 function_definition_allowed_p = false;
12963 }
12964
12965 /* Issue an error message if no declarators are present, and the
12966 decl-specifier-seq does not itself declare a class or
12967 enumeration: [dcl.dcl]/3. */
12968 if (!saw_declarator)
12969 {
12970 if (cp_parser_declares_only_class_p (parser))
12971 {
12972 if (!declares_class_or_enum
12973 && decl_specifiers.type
12974 && OVERLOAD_TYPE_P (decl_specifiers.type))
12975 /* Ensure an error is issued anyway when finish_decltype_type,
12976 called via cp_parser_decl_specifier_seq, returns a class or
12977 an enumeration (c++/51786). */
12978 decl_specifiers.type = NULL_TREE;
12979 shadow_tag (&decl_specifiers);
12980 }
12981 /* Perform any deferred access checks. */
12982 perform_deferred_access_checks (tf_warning_or_error);
12983 }
12984
12985 /* Consume the `;'. */
12986 finish:
12987 if (!maybe_range_for_decl)
12988 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12989 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12990 {
12991 if (init_loc != UNKNOWN_LOCATION)
12992 error_at (init_loc, "initializer in range-based %<for%> loop");
12993 if (comma_loc != UNKNOWN_LOCATION)
12994 error_at (comma_loc,
12995 "multiple declarations in range-based %<for%> loop");
12996 }
12997
12998 done:
12999 pop_deferring_access_checks ();
13000}
13001
13002/* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13003 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13004 initializer ; */
13005
13006static tree
13007cp_parser_decomposition_declaration (cp_parser *parser,
13008 cp_decl_specifier_seq *decl_specifiers,
13009 tree *maybe_range_for_decl,
13010 location_t *init_loc)
13011{
13012 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13013 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13014 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13015
13016 /* Parse the identifier-list. */
13017 auto_vec<cp_expr, 10> v;
13018 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13019 while (true)
13020 {
13021 cp_expr e = cp_parser_identifier (parser);
13022 if (e.get_value () == error_mark_node)
13023 break;
13024 v.safe_push (e);
13025 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13026 break;
13027 cp_lexer_consume_token (parser->lexer);
13028 }
13029
13030 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13031 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13032 {
13033 end_loc = UNKNOWN_LOCATION;
13034 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13035 false);
13036 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13037 cp_lexer_consume_token (parser->lexer);
13038 else
13039 {
13040 cp_parser_skip_to_end_of_statement (parser);
13041 return error_mark_node;
13042 }
13043 }
13044
13045 if (cxx_dialect < cxx1z)
13046 pedwarn (loc, 0, "decomposition declaration only available with "
13047 "-std=c++1z or -std=gnu++1z");
13048
13049 tree pushed_scope;
13050 cp_declarator *declarator = make_declarator (cdk_decomp);
13051 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13052 declarator->id_loc = loc;
13053 if (ref_qual != REF_QUAL_NONE)
13054 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13055 ref_qual == REF_QUAL_RVALUE,
13056 NULL_TREE);
13057 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13058 NULL_TREE, decl_specifiers->attributes,
13059 &pushed_scope);
13060 tree orig_decl = decl;
13061
13062 unsigned int i;
13063 cp_expr e;
13064 cp_decl_specifier_seq decl_specs;
13065 clear_decl_specs (&decl_specs);
13066 decl_specs.type = make_auto ();
13067 tree prev = decl;
13068 FOR_EACH_VEC_ELT (v, i, e)
13069 {
13070 if (i == 0)
13071 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13072 else
13073 declarator->u.id.unqualified_name = e.get_value ();
13074 declarator->id_loc = e.get_location ();
13075 tree elt_pushed_scope;
13076 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13077 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13078 if (decl2 == error_mark_node)
13079 decl = error_mark_node;
13080 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13081 {
13082 /* Ensure we've diagnosed redeclaration if we aren't creating
13083 a new VAR_DECL. */
13084 gcc_assert (errorcount);
13085 decl = error_mark_node;
13086 }
13087 else
13088 prev = decl2;
13089 if (elt_pushed_scope)
13090 pop_scope (elt_pushed_scope);
13091 }
13092
13093 if (v.is_empty ())
13094 {
13095 error_at (loc, "empty decomposition declaration");
13096 decl = error_mark_node;
13097 }
13098
13099 if (maybe_range_for_decl == NULL
13100 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13101 {
13102 bool non_constant_p = false, is_direct_init = false;
13103 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13104 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13105 &non_constant_p);
13106 if (initializer == NULL_TREE
13107 || (TREE_CODE (initializer) == TREE_LIST
13108 && TREE_CHAIN (initializer))
13109 || (is_direct_init
13110 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13111 && CONSTRUCTOR_NELTS (initializer) != 1))
13112 {
13113 error_at (loc, "invalid initializer for structured binding "
13114 "declaration");
13115 initializer = error_mark_node;
13116 }
13117
13118 if (decl != error_mark_node)
13119 {
13120 cp_maybe_mangle_decomp (decl, prev, v.length ());
13121 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13122 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13123 cp_finish_decomp (decl, prev, v.length ());
13124 }
13125 }
13126 else if (decl != error_mark_node)
13127 {
13128 *maybe_range_for_decl = prev;
13129 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13130 the underlying DECL. */
13131 cp_finish_decomp (decl, prev, v.length ());
13132 }
13133
13134 if (pushed_scope)
13135 pop_scope (pushed_scope);
13136
13137 if (decl == error_mark_node && DECL_P (orig_decl))
13138 {
13139 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13140 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13141 }
13142
13143 return decl;
13144}
13145
13146/* Parse a decl-specifier-seq.
13147
13148 decl-specifier-seq:
13149 decl-specifier-seq [opt] decl-specifier
13150 decl-specifier attribute-specifier-seq [opt] (C++11)
13151
13152 decl-specifier:
13153 storage-class-specifier
13154 type-specifier
13155 function-specifier
13156 friend
13157 typedef
13158
13159 GNU Extension:
13160
13161 decl-specifier:
13162 attributes
13163
13164 Concepts Extension:
13165
13166 decl-specifier:
13167 concept
13168
13169 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13170
13171 The parser flags FLAGS is used to control type-specifier parsing.
13172
13173 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13174 flags:
13175
13176 1: one of the decl-specifiers is an elaborated-type-specifier
13177 (i.e., a type declaration)
13178 2: one of the decl-specifiers is an enum-specifier or a
13179 class-specifier (i.e., a type definition)
13180
13181 */
13182
13183static void
13184cp_parser_decl_specifier_seq (cp_parser* parser,
13185 cp_parser_flags flags,
13186 cp_decl_specifier_seq *decl_specs,
13187 int* declares_class_or_enum)
13188{
13189 bool constructor_possible_p = !parser->in_declarator_p;
13190 bool found_decl_spec = false;
13191 cp_token *start_token = NULL;
13192 cp_decl_spec ds;
13193
13194 /* Clear DECL_SPECS. */
13195 clear_decl_specs (decl_specs);
13196
13197 /* Assume no class or enumeration type is declared. */
13198 *declares_class_or_enum = 0;
13199
13200 /* Keep reading specifiers until there are no more to read. */
13201 while (true)
13202 {
13203 bool constructor_p;
13204 cp_token *token;
13205 ds = ds_last;
13206
13207 /* Peek at the next token. */
13208 token = cp_lexer_peek_token (parser->lexer);
13209
13210 /* Save the first token of the decl spec list for error
13211 reporting. */
13212 if (!start_token)
13213 start_token = token;
13214 /* Handle attributes. */
13215 if (cp_next_tokens_can_be_attribute_p (parser))
13216 {
13217 /* Parse the attributes. */
13218 tree attrs = cp_parser_attributes_opt (parser);
13219
13220 /* In a sequence of declaration specifiers, c++11 attributes
13221 appertain to the type that precede them. In that case
13222 [dcl.spec]/1 says:
13223
13224 The attribute-specifier-seq affects the type only for
13225 the declaration it appears in, not other declarations
13226 involving the same type.
13227
13228 But for now let's force the user to position the
13229 attribute either at the beginning of the declaration or
13230 after the declarator-id, which would clearly mean that it
13231 applies to the declarator. */
13232 if (cxx11_attribute_p (attrs))
13233 {
13234 if (!found_decl_spec)
13235 /* The c++11 attribute is at the beginning of the
13236 declaration. It appertains to the entity being
13237 declared. */;
13238 else
13239 {
13240 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13241 {
13242 /* This is an attribute following a
13243 class-specifier. */
13244 if (decl_specs->type_definition_p)
13245 warn_misplaced_attr_for_class_type (token->location,
13246 decl_specs->type);
13247 attrs = NULL_TREE;
13248 }
13249 else
13250 {
13251 decl_specs->std_attributes
13252 = attr_chainon (decl_specs->std_attributes, attrs);
13253 if (decl_specs->locations[ds_std_attribute] == 0)
13254 decl_specs->locations[ds_std_attribute] = token->location;
13255 }
13256 continue;
13257 }
13258 }
13259
13260 decl_specs->attributes
13261 = attr_chainon (decl_specs->attributes, attrs);
13262 if (decl_specs->locations[ds_attribute] == 0)
13263 decl_specs->locations[ds_attribute] = token->location;
13264 continue;
13265 }
13266 /* Assume we will find a decl-specifier keyword. */
13267 found_decl_spec = true;
13268 /* If the next token is an appropriate keyword, we can simply
13269 add it to the list. */
13270 switch (token->keyword)
13271 {
13272 /* decl-specifier:
13273 friend
13274 constexpr */
13275 case RID_FRIEND:
13276 if (!at_class_scope_p ())
13277 {
13278 error_at (token->location, "%<friend%> used outside of class");
13279 cp_lexer_purge_token (parser->lexer);
13280 }
13281 else
13282 {
13283 ds = ds_friend;
13284 /* Consume the token. */
13285 cp_lexer_consume_token (parser->lexer);
13286 }
13287 break;
13288
13289 case RID_CONSTEXPR:
13290 ds = ds_constexpr;
13291 cp_lexer_consume_token (parser->lexer);
13292 break;
13293
13294 case RID_CONCEPT:
13295 ds = ds_concept;
13296 cp_lexer_consume_token (parser->lexer);
13297 break;
13298
13299 /* function-specifier:
13300 inline
13301 virtual
13302 explicit */
13303 case RID_INLINE:
13304 case RID_VIRTUAL:
13305 case RID_EXPLICIT:
13306 cp_parser_function_specifier_opt (parser, decl_specs);
13307 break;
13308
13309 /* decl-specifier:
13310 typedef */
13311 case RID_TYPEDEF:
13312 ds = ds_typedef;
13313 /* Consume the token. */
13314 cp_lexer_consume_token (parser->lexer);
13315 /* A constructor declarator cannot appear in a typedef. */
13316 constructor_possible_p = false;
13317 /* The "typedef" keyword can only occur in a declaration; we
13318 may as well commit at this point. */
13319 cp_parser_commit_to_tentative_parse (parser);
13320
13321 if (decl_specs->storage_class != sc_none)
13322 decl_specs->conflicting_specifiers_p = true;
13323 break;
13324
13325 /* storage-class-specifier:
13326 auto
13327 register
13328 static
13329 extern
13330 mutable
13331
13332 GNU Extension:
13333 thread */
13334 case RID_AUTO:
13335 if (cxx_dialect == cxx98)
13336 {
13337 /* Consume the token. */
13338 cp_lexer_consume_token (parser->lexer);
13339
13340 /* Complain about `auto' as a storage specifier, if
13341 we're complaining about C++0x compatibility. */
13342 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13343 " changes meaning in C++11; please remove it");
13344
13345 /* Set the storage class anyway. */
13346 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13347 token);
13348 }
13349 else
13350 /* C++0x auto type-specifier. */
13351 found_decl_spec = false;
13352 break;
13353
13354 case RID_REGISTER:
13355 case RID_STATIC:
13356 case RID_EXTERN:
13357 case RID_MUTABLE:
13358 /* Consume the token. */
13359 cp_lexer_consume_token (parser->lexer);
13360 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13361 token);
13362 break;
13363 case RID_THREAD:
13364 /* Consume the token. */
13365 ds = ds_thread;
13366 cp_lexer_consume_token (parser->lexer);
13367 break;
13368
13369 default:
13370 /* We did not yet find a decl-specifier yet. */
13371 found_decl_spec = false;
13372 break;
13373 }
13374
13375 if (found_decl_spec
13376 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13377 && token->keyword != RID_CONSTEXPR)
13378 error ("decl-specifier invalid in condition");
13379
13380 if (found_decl_spec
13381 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13382 && token->keyword != RID_MUTABLE
13383 && token->keyword != RID_CONSTEXPR)
13384 error_at (token->location, "%qD invalid in lambda",
13385 ridpointers[token->keyword]);
13386
13387 if (ds != ds_last)
13388 set_and_check_decl_spec_loc (decl_specs, ds, token);
13389
13390 /* Constructors are a special case. The `S' in `S()' is not a
13391 decl-specifier; it is the beginning of the declarator. */
13392 constructor_p
13393 = (!found_decl_spec
13394 && constructor_possible_p
13395 && (cp_parser_constructor_declarator_p
13396 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13397
13398 /* If we don't have a DECL_SPEC yet, then we must be looking at
13399 a type-specifier. */
13400 if (!found_decl_spec && !constructor_p)
13401 {
13402 int decl_spec_declares_class_or_enum;
13403 bool is_cv_qualifier;
13404 tree type_spec;
13405
13406 type_spec
13407 = cp_parser_type_specifier (parser, flags,
13408 decl_specs,
13409 /*is_declaration=*/true,
13410 &decl_spec_declares_class_or_enum,
13411 &is_cv_qualifier);
13412 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13413
13414 /* If this type-specifier referenced a user-defined type
13415 (a typedef, class-name, etc.), then we can't allow any
13416 more such type-specifiers henceforth.
13417
13418 [dcl.spec]
13419
13420 The longest sequence of decl-specifiers that could
13421 possibly be a type name is taken as the
13422 decl-specifier-seq of a declaration. The sequence shall
13423 be self-consistent as described below.
13424
13425 [dcl.type]
13426
13427 As a general rule, at most one type-specifier is allowed
13428 in the complete decl-specifier-seq of a declaration. The
13429 only exceptions are the following:
13430
13431 -- const or volatile can be combined with any other
13432 type-specifier.
13433
13434 -- signed or unsigned can be combined with char, long,
13435 short, or int.
13436
13437 -- ..
13438
13439 Example:
13440
13441 typedef char* Pc;
13442 void g (const int Pc);
13443
13444 Here, Pc is *not* part of the decl-specifier seq; it's
13445 the declarator. Therefore, once we see a type-specifier
13446 (other than a cv-qualifier), we forbid any additional
13447 user-defined types. We *do* still allow things like `int
13448 int' to be considered a decl-specifier-seq, and issue the
13449 error message later. */
13450 if (type_spec && !is_cv_qualifier)
13451 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13452 /* A constructor declarator cannot follow a type-specifier. */
13453 if (type_spec)
13454 {
13455 constructor_possible_p = false;
13456 found_decl_spec = true;
13457 if (!is_cv_qualifier)
13458 decl_specs->any_type_specifiers_p = true;
13459 }
13460 }
13461
13462 /* If we still do not have a DECL_SPEC, then there are no more
13463 decl-specifiers. */
13464 if (!found_decl_spec)
13465 break;
13466
13467 decl_specs->any_specifiers_p = true;
13468 /* After we see one decl-specifier, further decl-specifiers are
13469 always optional. */
13470 flags |= CP_PARSER_FLAGS_OPTIONAL;
13471 }
13472
13473 /* Don't allow a friend specifier with a class definition. */
13474 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13475 && (*declares_class_or_enum & 2))
13476 error_at (decl_specs->locations[ds_friend],
13477 "class definition may not be declared a friend");
13478}
13479
13480/* Parse an (optional) storage-class-specifier.
13481
13482 storage-class-specifier:
13483 auto
13484 register
13485 static
13486 extern
13487 mutable
13488
13489 GNU Extension:
13490
13491 storage-class-specifier:
13492 thread
13493
13494 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13495
13496static tree
13497cp_parser_storage_class_specifier_opt (cp_parser* parser)
13498{
13499 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13500 {
13501 case RID_AUTO:
13502 if (cxx_dialect != cxx98)
13503 return NULL_TREE;
13504 /* Fall through for C++98. */
13505 gcc_fallthrough ();
13506
13507 case RID_REGISTER:
13508 case RID_STATIC:
13509 case RID_EXTERN:
13510 case RID_MUTABLE:
13511 case RID_THREAD:
13512 /* Consume the token. */
13513 return cp_lexer_consume_token (parser->lexer)->u.value;
13514
13515 default:
13516 return NULL_TREE;
13517 }
13518}
13519
13520/* Parse an (optional) function-specifier.
13521
13522 function-specifier:
13523 inline
13524 virtual
13525 explicit
13526
13527 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13528 Updates DECL_SPECS, if it is non-NULL. */
13529
13530static tree
13531cp_parser_function_specifier_opt (cp_parser* parser,
13532 cp_decl_specifier_seq *decl_specs)
13533{
13534 cp_token *token = cp_lexer_peek_token (parser->lexer);
13535 switch (token->keyword)
13536 {
13537 case RID_INLINE:
13538 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13539 break;
13540
13541 case RID_VIRTUAL:
13542 /* 14.5.2.3 [temp.mem]
13543
13544 A member function template shall not be virtual. */
13545 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13546 && current_class_type)
13547 error_at (token->location, "templates may not be %<virtual%>");
13548 else
13549 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13550 break;
13551
13552 case RID_EXPLICIT:
13553 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13554 break;
13555
13556 default:
13557 return NULL_TREE;
13558 }
13559
13560 /* Consume the token. */
13561 return cp_lexer_consume_token (parser->lexer)->u.value;
13562}
13563
13564/* Parse a linkage-specification.
13565
13566 linkage-specification:
13567 extern string-literal { declaration-seq [opt] }
13568 extern string-literal declaration */
13569
13570static void
13571cp_parser_linkage_specification (cp_parser* parser)
13572{
13573 tree linkage;
13574
13575 /* Look for the `extern' keyword. */
13576 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13577
13578 /* Look for the string-literal. */
13579 linkage = cp_parser_string_literal (parser, false, false);
13580
13581 /* Transform the literal into an identifier. If the literal is a
13582 wide-character string, or contains embedded NULs, then we can't
13583 handle it as the user wants. */
13584 if (strlen (TREE_STRING_POINTER (linkage))
13585 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13586 {
13587 cp_parser_error (parser, "invalid linkage-specification");
13588 /* Assume C++ linkage. */
13589 linkage = lang_name_cplusplus;
13590 }
13591 else
13592 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13593
13594 /* We're now using the new linkage. */
13595 push_lang_context (linkage);
13596
13597 /* If the next token is a `{', then we're using the first
13598 production. */
13599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13600 {
13601 cp_ensure_no_omp_declare_simd (parser);
13602 cp_ensure_no_oacc_routine (parser);
13603
13604 /* Consume the `{' token. */
13605 cp_lexer_consume_token (parser->lexer);
13606 /* Parse the declarations. */
13607 cp_parser_declaration_seq_opt (parser);
13608 /* Look for the closing `}'. */
13609 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13610 }
13611 /* Otherwise, there's just one declaration. */
13612 else
13613 {
13614 bool saved_in_unbraced_linkage_specification_p;
13615
13616 saved_in_unbraced_linkage_specification_p
13617 = parser->in_unbraced_linkage_specification_p;
13618 parser->in_unbraced_linkage_specification_p = true;
13619 cp_parser_declaration (parser);
13620 parser->in_unbraced_linkage_specification_p
13621 = saved_in_unbraced_linkage_specification_p;
13622 }
13623
13624 /* We're done with the linkage-specification. */
13625 pop_lang_context ();
13626}
13627
13628/* Parse a static_assert-declaration.
13629
13630 static_assert-declaration:
13631 static_assert ( constant-expression , string-literal ) ;
13632 static_assert ( constant-expression ) ; (C++1Z)
13633
13634 If MEMBER_P, this static_assert is a class member. */
13635
13636static void
13637cp_parser_static_assert(cp_parser *parser, bool member_p)
13638{
13639 tree condition;
13640 tree message;
13641 cp_token *token;
13642 location_t saved_loc;
13643 bool dummy;
13644
13645 /* Peek at the `static_assert' token so we can keep track of exactly
13646 where the static assertion started. */
13647 token = cp_lexer_peek_token (parser->lexer);
13648 saved_loc = token->location;
13649
13650 /* Look for the `static_assert' keyword. */
13651 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13652 RT_STATIC_ASSERT))
13653 return;
13654
13655 /* We know we are in a static assertion; commit to any tentative
13656 parse. */
13657 if (cp_parser_parsing_tentatively (parser))
13658 cp_parser_commit_to_tentative_parse (parser);
13659
13660 /* Parse the `(' starting the static assertion condition. */
13661 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13662
13663 /* Parse the constant-expression. Allow a non-constant expression
13664 here in order to give better diagnostics in finish_static_assert. */
13665 condition =
13666 cp_parser_constant_expression (parser,
13667 /*allow_non_constant_p=*/true,
13668 /*non_constant_p=*/&dummy);
13669
13670 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13671 {
13672 if (cxx_dialect < cxx1z)
13673 pedwarn (input_location, OPT_Wpedantic,
13674 "static_assert without a message "
13675 "only available with -std=c++1z or -std=gnu++1z");
13676 /* Eat the ')' */
13677 cp_lexer_consume_token (parser->lexer);
13678 message = build_string (1, "");
13679 TREE_TYPE (message) = char_array_type_node;
13680 fix_string_type (message);
13681 }
13682 else
13683 {
13684 /* Parse the separating `,'. */
13685 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13686
13687 /* Parse the string-literal message. */
13688 message = cp_parser_string_literal (parser,
13689 /*translate=*/false,
13690 /*wide_ok=*/true);
13691
13692 /* A `)' completes the static assertion. */
13693 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13694 cp_parser_skip_to_closing_parenthesis (parser,
13695 /*recovering=*/true,
13696 /*or_comma=*/false,
13697 /*consume_paren=*/true);
13698 }
13699
13700 /* A semicolon terminates the declaration. */
13701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13702
13703 /* Complete the static assertion, which may mean either processing
13704 the static assert now or saving it for template instantiation. */
13705 finish_static_assert (condition, message, saved_loc, member_p);
13706}
13707
13708/* Parse the expression in decltype ( expression ). */
13709
13710static tree
13711cp_parser_decltype_expr (cp_parser *parser,
13712 bool &id_expression_or_member_access_p)
13713{
13714 cp_token *id_expr_start_token;
13715 tree expr;
13716
13717 /* Since we're going to preserve any side-effects from this parse, set up a
13718 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13719 in the expression. */
13720 tentative_firewall firewall (parser);
13721
13722 /* First, try parsing an id-expression. */
13723 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13724 cp_parser_parse_tentatively (parser);
13725 expr = cp_parser_id_expression (parser,
13726 /*template_keyword_p=*/false,
13727 /*check_dependency_p=*/true,
13728 /*template_p=*/NULL,
13729 /*declarator_p=*/false,
13730 /*optional_p=*/false);
13731
13732 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13733 {
13734 bool non_integral_constant_expression_p = false;
13735 tree id_expression = expr;
13736 cp_id_kind idk;
13737 const char *error_msg;
13738
13739 if (identifier_p (expr))
13740 /* Lookup the name we got back from the id-expression. */
13741 expr = cp_parser_lookup_name_simple (parser, expr,
13742 id_expr_start_token->location);
13743
13744 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
13745 /* A template without args is not a complete id-expression. */
13746 expr = error_mark_node;
13747
13748 if (expr
13749 && expr != error_mark_node
13750 && TREE_CODE (expr) != TYPE_DECL
13751 && (TREE_CODE (expr) != BIT_NOT_EXPR
13752 || !TYPE_P (TREE_OPERAND (expr, 0)))
13753 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13754 {
13755 /* Complete lookup of the id-expression. */
13756 expr = (finish_id_expression
13757 (id_expression, expr, parser->scope, &idk,
13758 /*integral_constant_expression_p=*/false,
13759 /*allow_non_integral_constant_expression_p=*/true,
13760 &non_integral_constant_expression_p,
13761 /*template_p=*/false,
13762 /*done=*/true,
13763 /*address_p=*/false,
13764 /*template_arg_p=*/false,
13765 &error_msg,
13766 id_expr_start_token->location));
13767
13768 if (expr == error_mark_node)
13769 /* We found an id-expression, but it was something that we
13770 should not have found. This is an error, not something
13771 we can recover from, so note that we found an
13772 id-expression and we'll recover as gracefully as
13773 possible. */
13774 id_expression_or_member_access_p = true;
13775 }
13776
13777 if (expr
13778 && expr != error_mark_node
13779 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13780 /* We have an id-expression. */
13781 id_expression_or_member_access_p = true;
13782 }
13783
13784 if (!id_expression_or_member_access_p)
13785 {
13786 /* Abort the id-expression parse. */
13787 cp_parser_abort_tentative_parse (parser);
13788
13789 /* Parsing tentatively, again. */
13790 cp_parser_parse_tentatively (parser);
13791
13792 /* Parse a class member access. */
13793 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13794 /*cast_p=*/false, /*decltype*/true,
13795 /*member_access_only_p=*/true, NULL);
13796
13797 if (expr
13798 && expr != error_mark_node
13799 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13800 /* We have an id-expression. */
13801 id_expression_or_member_access_p = true;
13802 }
13803
13804 if (id_expression_or_member_access_p)
13805 /* We have parsed the complete id-expression or member access. */
13806 cp_parser_parse_definitely (parser);
13807 else
13808 {
13809 /* Abort our attempt to parse an id-expression or member access
13810 expression. */
13811 cp_parser_abort_tentative_parse (parser);
13812
13813 /* Commit to the tentative_firewall so we get syntax errors. */
13814 cp_parser_commit_to_tentative_parse (parser);
13815
13816 /* Parse a full expression. */
13817 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13818 /*decltype_p=*/true);
13819 }
13820
13821 return expr;
13822}
13823
13824/* Parse a `decltype' type. Returns the type.
13825
13826 simple-type-specifier:
13827 decltype ( expression )
13828 C++14 proposal:
13829 decltype ( auto ) */
13830
13831static tree
13832cp_parser_decltype (cp_parser *parser)
13833{
13834 tree expr;
13835 bool id_expression_or_member_access_p = false;
13836 const char *saved_message;
13837 bool saved_integral_constant_expression_p;
13838 bool saved_non_integral_constant_expression_p;
13839 bool saved_greater_than_is_operator_p;
13840 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13841
13842 if (start_token->type == CPP_DECLTYPE)
13843 {
13844 /* Already parsed. */
13845 cp_lexer_consume_token (parser->lexer);
13846 return saved_checks_value (start_token->u.tree_check_value);
13847 }
13848
13849 /* Look for the `decltype' token. */
13850 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13851 return error_mark_node;
13852
13853 /* Parse the opening `('. */
13854 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13855 return error_mark_node;
13856
13857 /* decltype (auto) */
13858 if (cxx_dialect >= cxx14
13859 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13860 {
13861 cp_lexer_consume_token (parser->lexer);
13862 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13863 return error_mark_node;
13864 expr = make_decltype_auto ();
13865 AUTO_IS_DECLTYPE (expr) = true;
13866 goto rewrite;
13867 }
13868
13869 /* Types cannot be defined in a `decltype' expression. Save away the
13870 old message. */
13871 saved_message = parser->type_definition_forbidden_message;
13872
13873 /* And create the new one. */
13874 parser->type_definition_forbidden_message
13875 = G_("types may not be defined in %<decltype%> expressions");
13876
13877 /* The restrictions on constant-expressions do not apply inside
13878 decltype expressions. */
13879 saved_integral_constant_expression_p
13880 = parser->integral_constant_expression_p;
13881 saved_non_integral_constant_expression_p
13882 = parser->non_integral_constant_expression_p;
13883 parser->integral_constant_expression_p = false;
13884
13885 /* Within a parenthesized expression, a `>' token is always
13886 the greater-than operator. */
13887 saved_greater_than_is_operator_p
13888 = parser->greater_than_is_operator_p;
13889 parser->greater_than_is_operator_p = true;
13890
13891 /* Do not actually evaluate the expression. */
13892 ++cp_unevaluated_operand;
13893
13894 /* Do not warn about problems with the expression. */
13895 ++c_inhibit_evaluation_warnings;
13896
13897 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13898
13899 /* Go back to evaluating expressions. */
13900 --cp_unevaluated_operand;
13901 --c_inhibit_evaluation_warnings;
13902
13903 /* The `>' token might be the end of a template-id or
13904 template-parameter-list now. */
13905 parser->greater_than_is_operator_p
13906 = saved_greater_than_is_operator_p;
13907
13908 /* Restore the old message and the integral constant expression
13909 flags. */
13910 parser->type_definition_forbidden_message = saved_message;
13911 parser->integral_constant_expression_p
13912 = saved_integral_constant_expression_p;
13913 parser->non_integral_constant_expression_p
13914 = saved_non_integral_constant_expression_p;
13915
13916 /* Parse to the closing `)'. */
13917 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13918 {
13919 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13920 /*consume_paren=*/true);
13921 return error_mark_node;
13922 }
13923
13924 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13925 tf_warning_or_error);
13926
13927 rewrite:
13928 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13929 it again. */
13930 start_token->type = CPP_DECLTYPE;
13931 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13932 start_token->u.tree_check_value->value = expr;
13933 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13934 start_token->keyword = RID_MAX;
13935 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13936
13937 return expr;
13938}
13939
13940/* Special member functions [gram.special] */
13941
13942/* Parse a conversion-function-id.
13943
13944 conversion-function-id:
13945 operator conversion-type-id
13946
13947 Returns an IDENTIFIER_NODE representing the operator. */
13948
13949static tree
13950cp_parser_conversion_function_id (cp_parser* parser)
13951{
13952 tree type;
13953 tree saved_scope;
13954 tree saved_qualifying_scope;
13955 tree saved_object_scope;
13956 tree pushed_scope = NULL_TREE;
13957
13958 /* Look for the `operator' token. */
13959 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13960 return error_mark_node;
13961 /* When we parse the conversion-type-id, the current scope will be
13962 reset. However, we need that information in able to look up the
13963 conversion function later, so we save it here. */
13964 saved_scope = parser->scope;
13965 saved_qualifying_scope = parser->qualifying_scope;
13966 saved_object_scope = parser->object_scope;
13967 /* We must enter the scope of the class so that the names of
13968 entities declared within the class are available in the
13969 conversion-type-id. For example, consider:
13970
13971 struct S {
13972 typedef int I;
13973 operator I();
13974 };
13975
13976 S::operator I() { ... }
13977
13978 In order to see that `I' is a type-name in the definition, we
13979 must be in the scope of `S'. */
13980 if (saved_scope)
13981 pushed_scope = push_scope (saved_scope);
13982 /* Parse the conversion-type-id. */
13983 type = cp_parser_conversion_type_id (parser);
13984 /* Leave the scope of the class, if any. */
13985 if (pushed_scope)
13986 pop_scope (pushed_scope);
13987 /* Restore the saved scope. */
13988 parser->scope = saved_scope;
13989 parser->qualifying_scope = saved_qualifying_scope;
13990 parser->object_scope = saved_object_scope;
13991 /* If the TYPE is invalid, indicate failure. */
13992 if (type == error_mark_node)
13993 return error_mark_node;
13994 return mangle_conv_op_name_for_type (type);
13995}
13996
13997/* Parse a conversion-type-id:
13998
13999 conversion-type-id:
14000 type-specifier-seq conversion-declarator [opt]
14001
14002 Returns the TYPE specified. */
14003
14004static tree
14005cp_parser_conversion_type_id (cp_parser* parser)
14006{
14007 tree attributes;
14008 cp_decl_specifier_seq type_specifiers;
14009 cp_declarator *declarator;
14010 tree type_specified;
14011 const char *saved_message;
14012
14013 /* Parse the attributes. */
14014 attributes = cp_parser_attributes_opt (parser);
14015
14016 saved_message = parser->type_definition_forbidden_message;
14017 parser->type_definition_forbidden_message
14018 = G_("types may not be defined in a conversion-type-id");
14019
14020 /* Parse the type-specifiers. */
14021 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14022 /*is_trailing_return=*/false,
14023 &type_specifiers);
14024
14025 parser->type_definition_forbidden_message = saved_message;
14026
14027 /* If that didn't work, stop. */
14028 if (type_specifiers.type == error_mark_node)
14029 return error_mark_node;
14030 /* Parse the conversion-declarator. */
14031 declarator = cp_parser_conversion_declarator_opt (parser);
14032
14033 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14034 /*initialized=*/0, &attributes);
14035 if (attributes)
14036 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14037
14038 /* Don't give this error when parsing tentatively. This happens to
14039 work because we always parse this definitively once. */
14040 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14041 && type_uses_auto (type_specified))
14042 {
14043 if (cxx_dialect < cxx14)
14044 {
14045 error ("invalid use of %<auto%> in conversion operator");
14046 return error_mark_node;
14047 }
14048 else if (template_parm_scope_p ())
14049 warning (0, "use of %<auto%> in member template "
14050 "conversion operator can never be deduced");
14051 }
14052
14053 return type_specified;
14054}
14055
14056/* Parse an (optional) conversion-declarator.
14057
14058 conversion-declarator:
14059 ptr-operator conversion-declarator [opt]
14060
14061 */
14062
14063static cp_declarator *
14064cp_parser_conversion_declarator_opt (cp_parser* parser)
14065{
14066 enum tree_code code;
14067 tree class_type, std_attributes = NULL_TREE;
14068 cp_cv_quals cv_quals;
14069
14070 /* We don't know if there's a ptr-operator next, or not. */
14071 cp_parser_parse_tentatively (parser);
14072 /* Try the ptr-operator. */
14073 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14074 &std_attributes);
14075 /* If it worked, look for more conversion-declarators. */
14076 if (cp_parser_parse_definitely (parser))
14077 {
14078 cp_declarator *declarator;
14079
14080 /* Parse another optional declarator. */
14081 declarator = cp_parser_conversion_declarator_opt (parser);
14082
14083 declarator = cp_parser_make_indirect_declarator
14084 (code, class_type, cv_quals, declarator, std_attributes);
14085
14086 return declarator;
14087 }
14088
14089 return NULL;
14090}
14091
14092/* Parse an (optional) ctor-initializer.
14093
14094 ctor-initializer:
14095 : mem-initializer-list
14096
14097 Returns TRUE iff the ctor-initializer was actually present. */
14098
14099static bool
14100cp_parser_ctor_initializer_opt (cp_parser* parser)
14101{
14102 /* If the next token is not a `:', then there is no
14103 ctor-initializer. */
14104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14105 {
14106 /* Do default initialization of any bases and members. */
14107 if (DECL_CONSTRUCTOR_P (current_function_decl))
14108 finish_mem_initializers (NULL_TREE);
14109
14110 return false;
14111 }
14112
14113 /* Consume the `:' token. */
14114 cp_lexer_consume_token (parser->lexer);
14115 /* And the mem-initializer-list. */
14116 cp_parser_mem_initializer_list (parser);
14117
14118 return true;
14119}
14120
14121/* Parse a mem-initializer-list.
14122
14123 mem-initializer-list:
14124 mem-initializer ... [opt]
14125 mem-initializer ... [opt] , mem-initializer-list */
14126
14127static void
14128cp_parser_mem_initializer_list (cp_parser* parser)
14129{
14130 tree mem_initializer_list = NULL_TREE;
14131 tree target_ctor = error_mark_node;
14132 cp_token *token = cp_lexer_peek_token (parser->lexer);
14133
14134 /* Let the semantic analysis code know that we are starting the
14135 mem-initializer-list. */
14136 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14137 error_at (token->location,
14138 "only constructors take member initializers");
14139
14140 /* Loop through the list. */
14141 while (true)
14142 {
14143 tree mem_initializer;
14144
14145 token = cp_lexer_peek_token (parser->lexer);
14146 /* Parse the mem-initializer. */
14147 mem_initializer = cp_parser_mem_initializer (parser);
14148 /* If the next token is a `...', we're expanding member initializers. */
14149 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14150 {
14151 /* Consume the `...'. */
14152 cp_lexer_consume_token (parser->lexer);
14153
14154 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14155 can be expanded but members cannot. */
14156 if (mem_initializer != error_mark_node
14157 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14158 {
14159 error_at (token->location,
14160 "cannot expand initializer for member %<%D%>",
14161 TREE_PURPOSE (mem_initializer));
14162 mem_initializer = error_mark_node;
14163 }
14164
14165 /* Construct the pack expansion type. */
14166 if (mem_initializer != error_mark_node)
14167 mem_initializer = make_pack_expansion (mem_initializer);
14168 }
14169 if (target_ctor != error_mark_node
14170 && mem_initializer != error_mark_node)
14171 {
14172 error ("mem-initializer for %qD follows constructor delegation",
14173 TREE_PURPOSE (mem_initializer));
14174 mem_initializer = error_mark_node;
14175 }
14176 /* Look for a target constructor. */
14177 if (mem_initializer != error_mark_node
14178 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14179 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14180 {
14181 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14182 if (mem_initializer_list)
14183 {
14184 error ("constructor delegation follows mem-initializer for %qD",
14185 TREE_PURPOSE (mem_initializer_list));
14186 mem_initializer = error_mark_node;
14187 }
14188 target_ctor = mem_initializer;
14189 }
14190 /* Add it to the list, unless it was erroneous. */
14191 if (mem_initializer != error_mark_node)
14192 {
14193 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14194 mem_initializer_list = mem_initializer;
14195 }
14196 /* If the next token is not a `,', we're done. */
14197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14198 break;
14199 /* Consume the `,' token. */
14200 cp_lexer_consume_token (parser->lexer);
14201 }
14202
14203 /* Perform semantic analysis. */
14204 if (DECL_CONSTRUCTOR_P (current_function_decl))
14205 finish_mem_initializers (mem_initializer_list);
14206}
14207
14208/* Parse a mem-initializer.
14209
14210 mem-initializer:
14211 mem-initializer-id ( expression-list [opt] )
14212 mem-initializer-id braced-init-list
14213
14214 GNU extension:
14215
14216 mem-initializer:
14217 ( expression-list [opt] )
14218
14219 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14220 class) or FIELD_DECL (for a non-static data member) to initialize;
14221 the TREE_VALUE is the expression-list. An empty initialization
14222 list is represented by void_list_node. */
14223
14224static tree
14225cp_parser_mem_initializer (cp_parser* parser)
14226{
14227 tree mem_initializer_id;
14228 tree expression_list;
14229 tree member;
14230 cp_token *token = cp_lexer_peek_token (parser->lexer);
14231
14232 /* Find out what is being initialized. */
14233 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14234 {
14235 permerror (token->location,
14236 "anachronistic old-style base class initializer");
14237 mem_initializer_id = NULL_TREE;
14238 }
14239 else
14240 {
14241 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14242 if (mem_initializer_id == error_mark_node)
14243 return mem_initializer_id;
14244 }
14245 member = expand_member_init (mem_initializer_id);
14246 if (member && !DECL_P (member))
14247 in_base_initializer = 1;
14248
14249 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14250 {
14251 bool expr_non_constant_p;
14252 cp_lexer_set_source_position (parser->lexer);
14253 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14254 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14255 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14256 expression_list = build_tree_list (NULL_TREE, expression_list);
14257 }
14258 else
14259 {
14260 vec<tree, va_gc> *vec;
14261 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14262 /*cast_p=*/false,
14263 /*allow_expansion_p=*/true,
14264 /*non_constant_p=*/NULL);
14265 if (vec == NULL)
14266 return error_mark_node;
14267 expression_list = build_tree_list_vec (vec);
14268 release_tree_vector (vec);
14269 }
14270
14271 if (expression_list == error_mark_node)
14272 return error_mark_node;
14273 if (!expression_list)
14274 expression_list = void_type_node;
14275
14276 in_base_initializer = 0;
14277
14278 return member ? build_tree_list (member, expression_list) : error_mark_node;
14279}
14280
14281/* Parse a mem-initializer-id.
14282
14283 mem-initializer-id:
14284 :: [opt] nested-name-specifier [opt] class-name
14285 decltype-specifier (C++11)
14286 identifier
14287
14288 Returns a TYPE indicating the class to be initialized for the first
14289 production (and the second in C++11). Returns an IDENTIFIER_NODE
14290 indicating the data member to be initialized for the last production. */
14291
14292static tree
14293cp_parser_mem_initializer_id (cp_parser* parser)
14294{
14295 bool global_scope_p;
14296 bool nested_name_specifier_p;
14297 bool template_p = false;
14298 tree id;
14299
14300 cp_token *token = cp_lexer_peek_token (parser->lexer);
14301
14302 /* `typename' is not allowed in this context ([temp.res]). */
14303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14304 {
14305 error_at (token->location,
14306 "keyword %<typename%> not allowed in this context (a qualified "
14307 "member initializer is implicitly a type)");
14308 cp_lexer_consume_token (parser->lexer);
14309 }
14310 /* Look for the optional `::' operator. */
14311 global_scope_p
14312 = (cp_parser_global_scope_opt (parser,
14313 /*current_scope_valid_p=*/false)
14314 != NULL_TREE);
14315 /* Look for the optional nested-name-specifier. The simplest way to
14316 implement:
14317
14318 [temp.res]
14319
14320 The keyword `typename' is not permitted in a base-specifier or
14321 mem-initializer; in these contexts a qualified name that
14322 depends on a template-parameter is implicitly assumed to be a
14323 type name.
14324
14325 is to assume that we have seen the `typename' keyword at this
14326 point. */
14327 nested_name_specifier_p
14328 = (cp_parser_nested_name_specifier_opt (parser,
14329 /*typename_keyword_p=*/true,
14330 /*check_dependency_p=*/true,
14331 /*type_p=*/true,
14332 /*is_declaration=*/true)
14333 != NULL_TREE);
14334 if (nested_name_specifier_p)
14335 template_p = cp_parser_optional_template_keyword (parser);
14336 /* If there is a `::' operator or a nested-name-specifier, then we
14337 are definitely looking for a class-name. */
14338 if (global_scope_p || nested_name_specifier_p)
14339 return cp_parser_class_name (parser,
14340 /*typename_keyword_p=*/true,
14341 /*template_keyword_p=*/template_p,
14342 typename_type,
14343 /*check_dependency_p=*/true,
14344 /*class_head_p=*/false,
14345 /*is_declaration=*/true);
14346 /* Otherwise, we could also be looking for an ordinary identifier. */
14347 cp_parser_parse_tentatively (parser);
14348 if (cp_lexer_next_token_is_decltype (parser->lexer))
14349 /* Try a decltype-specifier. */
14350 id = cp_parser_decltype (parser);
14351 else
14352 /* Otherwise, try a class-name. */
14353 id = cp_parser_class_name (parser,
14354 /*typename_keyword_p=*/true,
14355 /*template_keyword_p=*/false,
14356 none_type,
14357 /*check_dependency_p=*/true,
14358 /*class_head_p=*/false,
14359 /*is_declaration=*/true);
14360 /* If we found one, we're done. */
14361 if (cp_parser_parse_definitely (parser))
14362 return id;
14363 /* Otherwise, look for an ordinary identifier. */
14364 return cp_parser_identifier (parser);
14365}
14366
14367/* Overloading [gram.over] */
14368
14369/* Parse an operator-function-id.
14370
14371 operator-function-id:
14372 operator operator
14373
14374 Returns an IDENTIFIER_NODE for the operator which is a
14375 human-readable spelling of the identifier, e.g., `operator +'. */
14376
14377static cp_expr
14378cp_parser_operator_function_id (cp_parser* parser)
14379{
14380 /* Look for the `operator' keyword. */
14381 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14382 return error_mark_node;
14383 /* And then the name of the operator itself. */
14384 return cp_parser_operator (parser);
14385}
14386
14387/* Return an identifier node for a user-defined literal operator.
14388 The suffix identifier is chained to the operator name identifier. */
14389
14390tree
14391cp_literal_operator_id (const char* name)
14392{
14393 tree identifier;
14394 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14395 + strlen (name) + 10);
14396 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14397 identifier = get_identifier (buffer);
14398
14399 return identifier;
14400}
14401
14402/* Parse an operator.
14403
14404 operator:
14405 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14406 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14407 || ++ -- , ->* -> () []
14408
14409 GNU Extensions:
14410
14411 operator:
14412 <? >? <?= >?=
14413
14414 Returns an IDENTIFIER_NODE for the operator which is a
14415 human-readable spelling of the identifier, e.g., `operator +'. */
14416
14417static cp_expr
14418cp_parser_operator (cp_parser* parser)
14419{
14420 tree id = NULL_TREE;
14421 cp_token *token;
14422 bool utf8 = false;
14423
14424 /* Peek at the next token. */
14425 token = cp_lexer_peek_token (parser->lexer);
14426
14427 location_t start_loc = token->location;
14428
14429 /* Figure out which operator we have. */
14430 switch (token->type)
14431 {
14432 case CPP_KEYWORD:
14433 {
14434 enum tree_code op;
14435
14436 /* The keyword should be either `new' or `delete'. */
14437 if (token->keyword == RID_NEW)
14438 op = NEW_EXPR;
14439 else if (token->keyword == RID_DELETE)
14440 op = DELETE_EXPR;
14441 else
14442 break;
14443
14444 /* Consume the `new' or `delete' token. */
14445 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14446
14447 /* Peek at the next token. */
14448 token = cp_lexer_peek_token (parser->lexer);
14449 /* If it's a `[' token then this is the array variant of the
14450 operator. */
14451 if (token->type == CPP_OPEN_SQUARE)
14452 {
14453 /* Consume the `[' token. */
14454 cp_lexer_consume_token (parser->lexer);
14455 /* Look for the `]' token. */
14456 if (cp_token *close_token
14457 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14458 end_loc = close_token->location;
14459 id = cp_operator_id (op == NEW_EXPR
14460 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14461 }
14462 /* Otherwise, we have the non-array variant. */
14463 else
14464 id = cp_operator_id (op);
14465
14466 location_t loc = make_location (start_loc, start_loc, end_loc);
14467
14468 return cp_expr (id, loc);
14469 }
14470
14471 case CPP_PLUS:
14472 id = cp_operator_id (PLUS_EXPR);
14473 break;
14474
14475 case CPP_MINUS:
14476 id = cp_operator_id (MINUS_EXPR);
14477 break;
14478
14479 case CPP_MULT:
14480 id = cp_operator_id (MULT_EXPR);
14481 break;
14482
14483 case CPP_DIV:
14484 id = cp_operator_id (TRUNC_DIV_EXPR);
14485 break;
14486
14487 case CPP_MOD:
14488 id = cp_operator_id (TRUNC_MOD_EXPR);
14489 break;
14490
14491 case CPP_XOR:
14492 id = cp_operator_id (BIT_XOR_EXPR);
14493 break;
14494
14495 case CPP_AND:
14496 id = cp_operator_id (BIT_AND_EXPR);
14497 break;
14498
14499 case CPP_OR:
14500 id = cp_operator_id (BIT_IOR_EXPR);
14501 break;
14502
14503 case CPP_COMPL:
14504 id = cp_operator_id (BIT_NOT_EXPR);
14505 break;
14506
14507 case CPP_NOT:
14508 id = cp_operator_id (TRUTH_NOT_EXPR);
14509 break;
14510
14511 case CPP_EQ:
14512 id = cp_assignment_operator_id (NOP_EXPR);
14513 break;
14514
14515 case CPP_LESS:
14516 id = cp_operator_id (LT_EXPR);
14517 break;
14518
14519 case CPP_GREATER:
14520 id = cp_operator_id (GT_EXPR);
14521 break;
14522
14523 case CPP_PLUS_EQ:
14524 id = cp_assignment_operator_id (PLUS_EXPR);
14525 break;
14526
14527 case CPP_MINUS_EQ:
14528 id = cp_assignment_operator_id (MINUS_EXPR);
14529 break;
14530
14531 case CPP_MULT_EQ:
14532 id = cp_assignment_operator_id (MULT_EXPR);
14533 break;
14534
14535 case CPP_DIV_EQ:
14536 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14537 break;
14538
14539 case CPP_MOD_EQ:
14540 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14541 break;
14542
14543 case CPP_XOR_EQ:
14544 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14545 break;
14546
14547 case CPP_AND_EQ:
14548 id = cp_assignment_operator_id (BIT_AND_EXPR);
14549 break;
14550
14551 case CPP_OR_EQ:
14552 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14553 break;
14554
14555 case CPP_LSHIFT:
14556 id = cp_operator_id (LSHIFT_EXPR);
14557 break;
14558
14559 case CPP_RSHIFT:
14560 id = cp_operator_id (RSHIFT_EXPR);
14561 break;
14562
14563 case CPP_LSHIFT_EQ:
14564 id = cp_assignment_operator_id (LSHIFT_EXPR);
14565 break;
14566
14567 case CPP_RSHIFT_EQ:
14568 id = cp_assignment_operator_id (RSHIFT_EXPR);
14569 break;
14570
14571 case CPP_EQ_EQ:
14572 id = cp_operator_id (EQ_EXPR);
14573 break;
14574
14575 case CPP_NOT_EQ:
14576 id = cp_operator_id (NE_EXPR);
14577 break;
14578
14579 case CPP_LESS_EQ:
14580 id = cp_operator_id (LE_EXPR);
14581 break;
14582
14583 case CPP_GREATER_EQ:
14584 id = cp_operator_id (GE_EXPR);
14585 break;
14586
14587 case CPP_AND_AND:
14588 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14589 break;
14590
14591 case CPP_OR_OR:
14592 id = cp_operator_id (TRUTH_ORIF_EXPR);
14593 break;
14594
14595 case CPP_PLUS_PLUS:
14596 id = cp_operator_id (POSTINCREMENT_EXPR);
14597 break;
14598
14599 case CPP_MINUS_MINUS:
14600 id = cp_operator_id (PREDECREMENT_EXPR);
14601 break;
14602
14603 case CPP_COMMA:
14604 id = cp_operator_id (COMPOUND_EXPR);
14605 break;
14606
14607 case CPP_DEREF_STAR:
14608 id = cp_operator_id (MEMBER_REF);
14609 break;
14610
14611 case CPP_DEREF:
14612 id = cp_operator_id (COMPONENT_REF);
14613 break;
14614
14615 case CPP_OPEN_PAREN:
14616 /* Consume the `('. */
14617 cp_lexer_consume_token (parser->lexer);
14618 /* Look for the matching `)'. */
14619 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14620 return cp_operator_id (CALL_EXPR);
14621
14622 case CPP_OPEN_SQUARE:
14623 /* Consume the `['. */
14624 cp_lexer_consume_token (parser->lexer);
14625 /* Look for the matching `]'. */
14626 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14627 return cp_operator_id (ARRAY_REF);
14628
14629 case CPP_UTF8STRING:
14630 case CPP_UTF8STRING_USERDEF:
14631 utf8 = true;
14632 /* FALLTHRU */
14633 case CPP_STRING:
14634 case CPP_WSTRING:
14635 case CPP_STRING16:
14636 case CPP_STRING32:
14637 case CPP_STRING_USERDEF:
14638 case CPP_WSTRING_USERDEF:
14639 case CPP_STRING16_USERDEF:
14640 case CPP_STRING32_USERDEF:
14641 {
14642 tree str, string_tree;
14643 int sz, len;
14644
14645 if (cxx_dialect == cxx98)
14646 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14647
14648 /* Consume the string. */
14649 str = cp_parser_string_literal (parser, /*translate=*/true,
14650 /*wide_ok=*/true, /*lookup_udlit=*/false);
14651 if (str == error_mark_node)
14652 return error_mark_node;
14653 else if (TREE_CODE (str) == USERDEF_LITERAL)
14654 {
14655 string_tree = USERDEF_LITERAL_VALUE (str);
14656 id = USERDEF_LITERAL_SUFFIX_ID (str);
14657 }
14658 else
14659 {
14660 string_tree = str;
14661 /* Look for the suffix identifier. */
14662 token = cp_lexer_peek_token (parser->lexer);
14663 if (token->type == CPP_NAME)
14664 id = cp_parser_identifier (parser);
14665 else if (token->type == CPP_KEYWORD)
14666 {
14667 error ("unexpected keyword;"
14668 " remove space between quotes and suffix identifier");
14669 return error_mark_node;
14670 }
14671 else
14672 {
14673 error ("expected suffix identifier");
14674 return error_mark_node;
14675 }
14676 }
14677 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14678 (TREE_TYPE (TREE_TYPE (string_tree))));
14679 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14680 if (len != 0)
14681 {
14682 error ("expected empty string after %<operator%> keyword");
14683 return error_mark_node;
14684 }
14685 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14686 != char_type_node)
14687 {
14688 error ("invalid encoding prefix in literal operator");
14689 return error_mark_node;
14690 }
14691 if (id != error_mark_node)
14692 {
14693 const char *name = IDENTIFIER_POINTER (id);
14694 id = cp_literal_operator_id (name);
14695 }
14696 return id;
14697 }
14698
14699 default:
14700 /* Anything else is an error. */
14701 break;
14702 }
14703
14704 /* If we have selected an identifier, we need to consume the
14705 operator token. */
14706 if (id)
14707 cp_lexer_consume_token (parser->lexer);
14708 /* Otherwise, no valid operator name was present. */
14709 else
14710 {
14711 cp_parser_error (parser, "expected operator");
14712 id = error_mark_node;
14713 }
14714
14715 return cp_expr (id, start_loc);
14716}
14717
14718/* Parse a template-declaration.
14719
14720 template-declaration:
14721 export [opt] template < template-parameter-list > declaration
14722
14723 If MEMBER_P is TRUE, this template-declaration occurs within a
14724 class-specifier.
14725
14726 The grammar rule given by the standard isn't correct. What
14727 is really meant is:
14728
14729 template-declaration:
14730 export [opt] template-parameter-list-seq
14731 decl-specifier-seq [opt] init-declarator [opt] ;
14732 export [opt] template-parameter-list-seq
14733 function-definition
14734
14735 template-parameter-list-seq:
14736 template-parameter-list-seq [opt]
14737 template < template-parameter-list >
14738
14739 Concept Extensions:
14740
14741 template-parameter-list-seq:
14742 template < template-parameter-list > requires-clause [opt]
14743
14744 requires-clause:
14745 requires logical-or-expression */
14746
14747static void
14748cp_parser_template_declaration (cp_parser* parser, bool member_p)
14749{
14750 /* Check for `export'. */
14751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14752 {
14753 /* Consume the `export' token. */
14754 cp_lexer_consume_token (parser->lexer);
14755 /* Warn that we do not support `export'. */
14756 warning (0, "keyword %<export%> not implemented, and will be ignored");
14757 }
14758
14759 cp_parser_template_declaration_after_export (parser, member_p);
14760}
14761
14762/* Parse a template-parameter-list.
14763
14764 template-parameter-list:
14765 template-parameter
14766 template-parameter-list , template-parameter
14767
14768 Returns a TREE_LIST. Each node represents a template parameter.
14769 The nodes are connected via their TREE_CHAINs. */
14770
14771static tree
14772cp_parser_template_parameter_list (cp_parser* parser)
14773{
14774 tree parameter_list = NULL_TREE;
14775
14776 begin_template_parm_list ();
14777
14778 /* The loop below parses the template parms. We first need to know
14779 the total number of template parms to be able to compute proper
14780 canonical types of each dependent type. So after the loop, when
14781 we know the total number of template parms,
14782 end_template_parm_list computes the proper canonical types and
14783 fixes up the dependent types accordingly. */
14784 while (true)
14785 {
14786 tree parameter;
14787 bool is_non_type;
14788 bool is_parameter_pack;
14789 location_t parm_loc;
14790
14791 /* Parse the template-parameter. */
14792 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14793 parameter = cp_parser_template_parameter (parser,
14794 &is_non_type,
14795 &is_parameter_pack);
14796 /* Add it to the list. */
14797 if (parameter != error_mark_node)
14798 parameter_list = process_template_parm (parameter_list,
14799 parm_loc,
14800 parameter,
14801 is_non_type,
14802 is_parameter_pack);
14803 else
14804 {
14805 tree err_parm = build_tree_list (parameter, parameter);
14806 parameter_list = chainon (parameter_list, err_parm);
14807 }
14808
14809 /* If the next token is not a `,', we're done. */
14810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14811 break;
14812 /* Otherwise, consume the `,' token. */
14813 cp_lexer_consume_token (parser->lexer);
14814 }
14815
14816 return end_template_parm_list (parameter_list);
14817}
14818
14819/* Parse a introduction-list.
14820
14821 introduction-list:
14822 introduced-parameter
14823 introduction-list , introduced-parameter
14824
14825 introduced-parameter:
14826 ...[opt] identifier
14827
14828 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14829 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14830 WILDCARD_DECL will also have DECL_NAME set and token location in
14831 DECL_SOURCE_LOCATION. */
14832
14833static tree
14834cp_parser_introduction_list (cp_parser *parser)
14835{
14836 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14837
14838 while (true)
14839 {
14840 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14841 if (is_pack)
14842 cp_lexer_consume_token (parser->lexer);
14843
14844 /* Build placeholder. */
14845 tree parm = build_nt (WILDCARD_DECL);
14846 DECL_SOURCE_LOCATION (parm)
14847 = cp_lexer_peek_token (parser->lexer)->location;
14848 DECL_NAME (parm) = cp_parser_identifier (parser);
14849 WILDCARD_PACK_P (parm) = is_pack;
14850 vec_safe_push (introduction_vec, parm);
14851
14852 /* If the next token is not a `,', we're done. */
14853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14854 break;
14855 /* Otherwise, consume the `,' token. */
14856 cp_lexer_consume_token (parser->lexer);
14857 }
14858
14859 /* Convert the vec into a TREE_VEC. */
14860 tree introduction_list = make_tree_vec (introduction_vec->length ());
14861 unsigned int n;
14862 tree parm;
14863 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14864 TREE_VEC_ELT (introduction_list, n) = parm;
14865
14866 release_tree_vector (introduction_vec);
14867 return introduction_list;
14868}
14869
14870/* Given a declarator, get the declarator-id part, or NULL_TREE if this
14871 is an abstract declarator. */
14872
14873static inline cp_declarator*
14874get_id_declarator (cp_declarator *declarator)
14875{
14876 cp_declarator *d = declarator;
14877 while (d && d->kind != cdk_id)
14878 d = d->declarator;
14879 return d;
14880}
14881
14882/* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14883 is an abstract declarator. */
14884
14885static inline tree
14886get_unqualified_id (cp_declarator *declarator)
14887{
14888 declarator = get_id_declarator (declarator);
14889 if (declarator)
14890 return declarator->u.id.unqualified_name;
14891 else
14892 return NULL_TREE;
14893}
14894
14895/* Returns true if DECL represents a constrained-parameter. */
14896
14897static inline bool
14898is_constrained_parameter (tree decl)
14899{
14900 return (decl
14901 && TREE_CODE (decl) == TYPE_DECL
14902 && CONSTRAINED_PARM_CONCEPT (decl)
14903 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14904}
14905
14906/* Returns true if PARM declares a constrained-parameter. */
14907
14908static inline bool
14909is_constrained_parameter (cp_parameter_declarator *parm)
14910{
14911 return is_constrained_parameter (parm->decl_specifiers.type);
14912}
14913
14914/* Check that the type parameter is only a declarator-id, and that its
14915 type is not cv-qualified. */
14916
14917bool
14918cp_parser_check_constrained_type_parm (cp_parser *parser,
14919 cp_parameter_declarator *parm)
14920{
14921 if (!parm->declarator)
14922 return true;
14923
14924 if (parm->declarator->kind != cdk_id)
14925 {
14926 cp_parser_error (parser, "invalid constrained type parameter");
14927 return false;
14928 }
14929
14930 /* Don't allow cv-qualified type parameters. */
14931 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14932 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14933 {
14934 cp_parser_error (parser, "cv-qualified type parameter");
14935 return false;
14936 }
14937
14938 return true;
14939}
14940
14941/* Finish parsing/processing a template type parameter and checking
14942 various restrictions. */
14943
14944static inline tree
14945cp_parser_constrained_type_template_parm (cp_parser *parser,
14946 tree id,
14947 cp_parameter_declarator* parmdecl)
14948{
14949 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14950 return finish_template_type_parm (class_type_node, id);
14951 else
14952 return error_mark_node;
14953}
14954
14955static tree
14956finish_constrained_template_template_parm (tree proto, tree id)
14957{
14958 /* FIXME: This should probably be copied, and we may need to adjust
14959 the template parameter depths. */
14960 tree saved_parms = current_template_parms;
14961 begin_template_parm_list ();
14962 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14963 end_template_parm_list ();
14964
14965 tree parm = finish_template_template_parm (class_type_node, id);
14966 current_template_parms = saved_parms;
14967
14968 return parm;
14969}
14970
14971/* Finish parsing/processing a template template parameter by borrowing
14972 the template parameter list from the prototype parameter. */
14973
14974static tree
14975cp_parser_constrained_template_template_parm (cp_parser *parser,
14976 tree proto,
14977 tree id,
14978 cp_parameter_declarator *parmdecl)
14979{
14980 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14981 return error_mark_node;
14982 return finish_constrained_template_template_parm (proto, id);
14983}
14984
14985/* Create a new non-type template parameter from the given PARM
14986 declarator. */
14987
14988static tree
14989constrained_non_type_template_parm (bool *is_non_type,
14990 cp_parameter_declarator *parm)
14991{
14992 *is_non_type = true;
14993 cp_declarator *decl = parm->declarator;
14994 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14995 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14996 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14997}
14998
14999/* Build a constrained template parameter based on the PARMDECL
15000 declarator. The type of PARMDECL is the constrained type, which
15001 refers to the prototype template parameter that ultimately
15002 specifies the type of the declared parameter. */
15003
15004static tree
15005finish_constrained_parameter (cp_parser *parser,
15006 cp_parameter_declarator *parmdecl,
15007 bool *is_non_type,
15008 bool *is_parameter_pack)
15009{
15010 tree decl = parmdecl->decl_specifiers.type;
15011 tree id = get_unqualified_id (parmdecl->declarator);
15012 tree def = parmdecl->default_argument;
15013 tree proto = DECL_INITIAL (decl);
15014
15015 /* A template parameter constrained by a variadic concept shall also
15016 be declared as a template parameter pack. */
15017 bool is_variadic = template_parameter_pack_p (proto);
15018 if (is_variadic && !*is_parameter_pack)
15019 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15020
15021 /* Build the parameter. Return an error if the declarator was invalid. */
15022 tree parm;
15023 if (TREE_CODE (proto) == TYPE_DECL)
15024 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15025 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15026 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15027 parmdecl);
15028 else
15029 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15030 if (parm == error_mark_node)
15031 return error_mark_node;
15032
15033 /* Finish the parameter decl and create a node attaching the
15034 default argument and constraint. */
15035 parm = build_tree_list (def, parm);
15036 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15037
15038 return parm;
15039}
15040
15041/* Returns true if the parsed type actually represents the declaration
15042 of a type template-parameter. */
15043
15044static inline bool
15045declares_constrained_type_template_parameter (tree type)
15046{
15047 return (is_constrained_parameter (type)
15048 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15049}
15050
15051
15052/* Returns true if the parsed type actually represents the declaration of
15053 a template template-parameter. */
15054
15055static bool
15056declares_constrained_template_template_parameter (tree type)
15057{
15058 return (is_constrained_parameter (type)
15059 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15060}
15061
15062/* Parse a default argument for a type template-parameter.
15063 Note that diagnostics are handled in cp_parser_template_parameter. */
15064
15065static tree
15066cp_parser_default_type_template_argument (cp_parser *parser)
15067{
15068 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15069
15070 /* Consume the `=' token. */
15071 cp_lexer_consume_token (parser->lexer);
15072
15073 cp_token *token = cp_lexer_peek_token (parser->lexer);
15074
15075 /* Parse the default-argument. */
15076 push_deferring_access_checks (dk_no_deferred);
15077 tree default_argument = cp_parser_type_id (parser);
15078 pop_deferring_access_checks ();
15079
15080 if (flag_concepts && type_uses_auto (default_argument))
15081 {
15082 error_at (token->location,
15083 "invalid use of %<auto%> in default template argument");
15084 return error_mark_node;
15085 }
15086
15087 return default_argument;
15088}
15089
15090/* Parse a default argument for a template template-parameter. */
15091
15092static tree
15093cp_parser_default_template_template_argument (cp_parser *parser)
15094{
15095 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15096
15097 bool is_template;
15098
15099 /* Consume the `='. */
15100 cp_lexer_consume_token (parser->lexer);
15101 /* Parse the id-expression. */
15102 push_deferring_access_checks (dk_no_deferred);
15103 /* save token before parsing the id-expression, for error
15104 reporting */
15105 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15106 tree default_argument
15107 = cp_parser_id_expression (parser,
15108 /*template_keyword_p=*/false,
15109 /*check_dependency_p=*/true,
15110 /*template_p=*/&is_template,
15111 /*declarator_p=*/false,
15112 /*optional_p=*/false);
15113 if (TREE_CODE (default_argument) == TYPE_DECL)
15114 /* If the id-expression was a template-id that refers to
15115 a template-class, we already have the declaration here,
15116 so no further lookup is needed. */
15117 ;
15118 else
15119 /* Look up the name. */
15120 default_argument
15121 = cp_parser_lookup_name (parser, default_argument,
15122 none_type,
15123 /*is_template=*/is_template,
15124 /*is_namespace=*/false,
15125 /*check_dependency=*/true,
15126 /*ambiguous_decls=*/NULL,
15127 token->location);
15128 /* See if the default argument is valid. */
15129 default_argument = check_template_template_default_arg (default_argument);
15130 pop_deferring_access_checks ();
15131 return default_argument;
15132}
15133
15134/* Parse a template-parameter.
15135
15136 template-parameter:
15137 type-parameter
15138 parameter-declaration
15139
15140 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15141 the parameter. The TREE_PURPOSE is the default value, if any.
15142 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15143 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15144 set to true iff this parameter is a parameter pack. */
15145
15146static tree
15147cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15148 bool *is_parameter_pack)
15149{
15150 cp_token *token;
15151 cp_parameter_declarator *parameter_declarator;
15152 tree parm;
15153
15154 /* Assume it is a type parameter or a template parameter. */
15155 *is_non_type = false;
15156 /* Assume it not a parameter pack. */
15157 *is_parameter_pack = false;
15158 /* Peek at the next token. */
15159 token = cp_lexer_peek_token (parser->lexer);
15160 /* If it is `class' or `template', we have a type-parameter. */
15161 if (token->keyword == RID_TEMPLATE)
15162 return cp_parser_type_parameter (parser, is_parameter_pack);
15163 /* If it is `class' or `typename' we do not know yet whether it is a
15164 type parameter or a non-type parameter. Consider:
15165
15166 template <typename T, typename T::X X> ...
15167
15168 or:
15169
15170 template <class C, class D*> ...
15171
15172 Here, the first parameter is a type parameter, and the second is
15173 a non-type parameter. We can tell by looking at the token after
15174 the identifier -- if it is a `,', `=', or `>' then we have a type
15175 parameter. */
15176 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15177 {
15178 /* Peek at the token after `class' or `typename'. */
15179 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15180 /* If it's an ellipsis, we have a template type parameter
15181 pack. */
15182 if (token->type == CPP_ELLIPSIS)
15183 return cp_parser_type_parameter (parser, is_parameter_pack);
15184 /* If it's an identifier, skip it. */
15185 if (token->type == CPP_NAME)
15186 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15187 /* Now, see if the token looks like the end of a template
15188 parameter. */
15189 if (token->type == CPP_COMMA
15190 || token->type == CPP_EQ
15191 || token->type == CPP_GREATER)
15192 return cp_parser_type_parameter (parser, is_parameter_pack);
15193 }
15194
15195 /* Otherwise, it is a non-type parameter or a constrained parameter.
15196
15197 [temp.param]
15198
15199 When parsing a default template-argument for a non-type
15200 template-parameter, the first non-nested `>' is taken as the end
15201 of the template parameter-list rather than a greater-than
15202 operator. */
15203 parameter_declarator
15204 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15205 /*parenthesized_p=*/NULL);
15206
15207 if (!parameter_declarator)
15208 return error_mark_node;
15209
15210 /* If the parameter declaration is marked as a parameter pack, set
15211 *IS_PARAMETER_PACK to notify the caller. */
15212 if (parameter_declarator->template_parameter_pack_p)
15213 *is_parameter_pack = true;
15214
15215 if (parameter_declarator->default_argument)
15216 {
15217 /* Can happen in some cases of erroneous input (c++/34892). */
15218 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15219 /* Consume the `...' for better error recovery. */
15220 cp_lexer_consume_token (parser->lexer);
15221 }
15222
15223 // The parameter may have been constrained.
15224 if (is_constrained_parameter (parameter_declarator))
15225 return finish_constrained_parameter (parser,
15226 parameter_declarator,
15227 is_non_type,
15228 is_parameter_pack);
15229
15230 // Now we're sure that the parameter is a non-type parameter.
15231 *is_non_type = true;
15232
15233 parm = grokdeclarator (parameter_declarator->declarator,
15234 &parameter_declarator->decl_specifiers,
15235 TPARM, /*initialized=*/0,
15236 /*attrlist=*/NULL);
15237 if (parm == error_mark_node)
15238 return error_mark_node;
15239
15240 return build_tree_list (parameter_declarator->default_argument, parm);
15241}
15242
15243/* Parse a type-parameter.
15244
15245 type-parameter:
15246 class identifier [opt]
15247 class identifier [opt] = type-id
15248 typename identifier [opt]
15249 typename identifier [opt] = type-id
15250 template < template-parameter-list > class identifier [opt]
15251 template < template-parameter-list > class identifier [opt]
15252 = id-expression
15253
15254 GNU Extension (variadic templates):
15255
15256 type-parameter:
15257 class ... identifier [opt]
15258 typename ... identifier [opt]
15259
15260 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15261 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15262 the declaration of the parameter.
15263
15264 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15265
15266static tree
15267cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15268{
15269 cp_token *token;
15270 tree parameter;
15271
15272 /* Look for a keyword to tell us what kind of parameter this is. */
15273 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15274 if (!token)
15275 return error_mark_node;
15276
15277 switch (token->keyword)
15278 {
15279 case RID_CLASS:
15280 case RID_TYPENAME:
15281 {
15282 tree identifier;
15283 tree default_argument;
15284
15285 /* If the next token is an ellipsis, we have a template
15286 argument pack. */
15287 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15288 {
15289 /* Consume the `...' token. */
15290 cp_lexer_consume_token (parser->lexer);
15291 maybe_warn_variadic_templates ();
15292
15293 *is_parameter_pack = true;
15294 }
15295
15296 /* If the next token is an identifier, then it names the
15297 parameter. */
15298 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15299 identifier = cp_parser_identifier (parser);
15300 else
15301 identifier = NULL_TREE;
15302
15303 /* Create the parameter. */
15304 parameter = finish_template_type_parm (class_type_node, identifier);
15305
15306 /* If the next token is an `=', we have a default argument. */
15307 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15308 {
15309 default_argument
15310 = cp_parser_default_type_template_argument (parser);
15311
15312 /* Template parameter packs cannot have default
15313 arguments. */
15314 if (*is_parameter_pack)
15315 {
15316 if (identifier)
15317 error_at (token->location,
15318 "template parameter pack %qD cannot have a "
15319 "default argument", identifier);
15320 else
15321 error_at (token->location,
15322 "template parameter packs cannot have "
15323 "default arguments");
15324 default_argument = NULL_TREE;
15325 }
15326 else if (check_for_bare_parameter_packs (default_argument))
15327 default_argument = error_mark_node;
15328 }
15329 else
15330 default_argument = NULL_TREE;
15331
15332 /* Create the combined representation of the parameter and the
15333 default argument. */
15334 parameter = build_tree_list (default_argument, parameter);
15335 }
15336 break;
15337
15338 case RID_TEMPLATE:
15339 {
15340 tree identifier;
15341 tree default_argument;
15342
15343 /* Look for the `<'. */
15344 cp_parser_require (parser, CPP_LESS, RT_LESS);
15345 /* Parse the template-parameter-list. */
15346 cp_parser_template_parameter_list (parser);
15347 /* Look for the `>'. */
15348 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15349
15350 // If template requirements are present, parse them.
15351 if (flag_concepts)
15352 {
15353 tree reqs = get_shorthand_constraints (current_template_parms);
15354 if (tree r = cp_parser_requires_clause_opt (parser))
15355 reqs = conjoin_constraints (reqs, normalize_expression (r));
15356 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15357 }
15358
15359 /* Look for the `class' or 'typename' keywords. */
15360 cp_parser_type_parameter_key (parser);
15361 /* If the next token is an ellipsis, we have a template
15362 argument pack. */
15363 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15364 {
15365 /* Consume the `...' token. */
15366 cp_lexer_consume_token (parser->lexer);
15367 maybe_warn_variadic_templates ();
15368
15369 *is_parameter_pack = true;
15370 }
15371 /* If the next token is an `=', then there is a
15372 default-argument. If the next token is a `>', we are at
15373 the end of the parameter-list. If the next token is a `,',
15374 then we are at the end of this parameter. */
15375 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15376 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15377 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15378 {
15379 identifier = cp_parser_identifier (parser);
15380 /* Treat invalid names as if the parameter were nameless. */
15381 if (identifier == error_mark_node)
15382 identifier = NULL_TREE;
15383 }
15384 else
15385 identifier = NULL_TREE;
15386
15387 /* Create the template parameter. */
15388 parameter = finish_template_template_parm (class_type_node,
15389 identifier);
15390
15391 /* If the next token is an `=', then there is a
15392 default-argument. */
15393 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15394 {
15395 default_argument
15396 = cp_parser_default_template_template_argument (parser);
15397
15398 /* Template parameter packs cannot have default
15399 arguments. */
15400 if (*is_parameter_pack)
15401 {
15402 if (identifier)
15403 error_at (token->location,
15404 "template parameter pack %qD cannot "
15405 "have a default argument",
15406 identifier);
15407 else
15408 error_at (token->location, "template parameter packs cannot "
15409 "have default arguments");
15410 default_argument = NULL_TREE;
15411 }
15412 }
15413 else
15414 default_argument = NULL_TREE;
15415
15416 /* Create the combined representation of the parameter and the
15417 default argument. */
15418 parameter = build_tree_list (default_argument, parameter);
15419 }
15420 break;
15421
15422 default:
15423 gcc_unreachable ();
15424 break;
15425 }
15426
15427 return parameter;
15428}
15429
15430/* Parse a template-id.
15431
15432 template-id:
15433 template-name < template-argument-list [opt] >
15434
15435 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15436 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15437 returned. Otherwise, if the template-name names a function, or set
15438 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15439 names a class, returns a TYPE_DECL for the specialization.
15440
15441 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15442 uninstantiated templates. */
15443
15444static tree
15445cp_parser_template_id (cp_parser *parser,
15446 bool template_keyword_p,
15447 bool check_dependency_p,
15448 enum tag_types tag_type,
15449 bool is_declaration)
15450{
15451 tree templ;
15452 tree arguments;
15453 tree template_id;
15454 cp_token_position start_of_id = 0;
15455 cp_token *next_token = NULL, *next_token_2 = NULL;
15456 bool is_identifier;
15457
15458 /* If the next token corresponds to a template-id, there is no need
15459 to reparse it. */
15460 next_token = cp_lexer_peek_token (parser->lexer);
15461 if (next_token->type == CPP_TEMPLATE_ID)
15462 {
15463 cp_lexer_consume_token (parser->lexer);
15464 return saved_checks_value (next_token->u.tree_check_value);
15465 }
15466
15467 /* Avoid performing name lookup if there is no possibility of
15468 finding a template-id. */
15469 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15470 || (next_token->type == CPP_NAME
15471 && !cp_parser_nth_token_starts_template_argument_list_p
15472 (parser, 2)))
15473 {
15474 cp_parser_error (parser, "expected template-id");
15475 return error_mark_node;
15476 }
15477
15478 /* Remember where the template-id starts. */
15479 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15480 start_of_id = cp_lexer_token_position (parser->lexer, false);
15481
15482 push_deferring_access_checks (dk_deferred);
15483
15484 /* Parse the template-name. */
15485 is_identifier = false;
15486 templ = cp_parser_template_name (parser, template_keyword_p,
15487 check_dependency_p,
15488 is_declaration,
15489 tag_type,
15490 &is_identifier);
15491 if (templ == error_mark_node || is_identifier)
15492 {
15493 pop_deferring_access_checks ();
15494 return templ;
15495 }
15496
15497 /* Since we're going to preserve any side-effects from this parse, set up a
15498 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15499 in the template arguments. */
15500 tentative_firewall firewall (parser);
15501
15502 /* If we find the sequence `[:' after a template-name, it's probably
15503 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15504 parse correctly the argument list. */
15505 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15506 == CPP_OPEN_SQUARE)
15507 && next_token->flags & DIGRAPH
15508 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15509 == CPP_COLON)
15510 && !(next_token_2->flags & PREV_WHITE))
15511 {
15512 cp_parser_parse_tentatively (parser);
15513 /* Change `:' into `::'. */
15514 next_token_2->type = CPP_SCOPE;
15515 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15516 CPP_LESS. */
15517 cp_lexer_consume_token (parser->lexer);
15518
15519 /* Parse the arguments. */
15520 arguments = cp_parser_enclosed_template_argument_list (parser);
15521 if (!cp_parser_parse_definitely (parser))
15522 {
15523 /* If we couldn't parse an argument list, then we revert our changes
15524 and return simply an error. Maybe this is not a template-id
15525 after all. */
15526 next_token_2->type = CPP_COLON;
15527 cp_parser_error (parser, "expected %<<%>");
15528 pop_deferring_access_checks ();
15529 return error_mark_node;
15530 }
15531 /* Otherwise, emit an error about the invalid digraph, but continue
15532 parsing because we got our argument list. */
15533 if (permerror (next_token->location,
15534 "%<<::%> cannot begin a template-argument list"))
15535 {
15536 static bool hint = false;
15537 inform (next_token->location,
15538 "%<<:%> is an alternate spelling for %<[%>."
15539 " Insert whitespace between %<<%> and %<::%>");
15540 if (!hint && !flag_permissive)
15541 {
15542 inform (next_token->location, "(if you use %<-fpermissive%> "
15543 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15544 "accept your code)");
15545 hint = true;
15546 }
15547 }
15548 }
15549 else
15550 {
15551 /* Look for the `<' that starts the template-argument-list. */
15552 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15553 {
15554 pop_deferring_access_checks ();
15555 return error_mark_node;
15556 }
15557 /* Parse the arguments. */
15558 arguments = cp_parser_enclosed_template_argument_list (parser);
15559 }
15560
15561 /* Build a representation of the specialization. */
15562 if (identifier_p (templ))
15563 template_id = build_min_nt_loc (next_token->location,
15564 TEMPLATE_ID_EXPR,
15565 templ, arguments);
15566 else if (DECL_TYPE_TEMPLATE_P (templ)
15567 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15568 {
15569 bool entering_scope;
15570 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15571 template (rather than some instantiation thereof) only if
15572 is not nested within some other construct. For example, in
15573 "template <typename T> void f(T) { A<T>::", A<T> is just an
15574 instantiation of A. */
15575 entering_scope = (template_parm_scope_p ()
15576 && cp_lexer_next_token_is (parser->lexer,
15577 CPP_SCOPE));
15578 template_id
15579 = finish_template_type (templ, arguments, entering_scope);
15580 }
15581 /* A template-like identifier may be a partial concept id. */
15582 else if (flag_concepts
15583 && (template_id = (cp_parser_maybe_partial_concept_id
15584 (parser, templ, arguments))))
15585 return template_id;
15586 else if (variable_template_p (templ))
15587 {
15588 template_id = lookup_template_variable (templ, arguments);
15589 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15590 SET_EXPR_LOCATION (template_id, next_token->location);
15591 }
15592 else
15593 {
15594 /* If it's not a class-template or a template-template, it should be
15595 a function-template. */
15596 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15597 || TREE_CODE (templ) == OVERLOAD
15598 || BASELINK_P (templ)));
15599
15600 template_id = lookup_template_function (templ, arguments);
15601 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15602 SET_EXPR_LOCATION (template_id, next_token->location);
15603 }
15604
15605 /* If parsing tentatively, replace the sequence of tokens that makes
15606 up the template-id with a CPP_TEMPLATE_ID token. That way,
15607 should we re-parse the token stream, we will not have to repeat
15608 the effort required to do the parse, nor will we issue duplicate
15609 error messages about problems during instantiation of the
15610 template. */
15611 if (start_of_id
15612 /* Don't do this if we had a parse error in a declarator; re-parsing
15613 might succeed if a name changes meaning (60361). */
15614 && !(cp_parser_error_occurred (parser)
15615 && cp_parser_parsing_tentatively (parser)
15616 && parser->in_declarator_p))
15617 {
15618 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15619
15620 /* Reset the contents of the START_OF_ID token. */
15621 token->type = CPP_TEMPLATE_ID;
15622
15623 /* Update the location to be of the form:
15624 template-name < template-argument-list [opt] >
15625 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15626 with caret == start at the start of the template-name,
15627 ranging until the closing '>'. */
15628 location_t finish_loc
15629 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15630 location_t combined_loc
15631 = make_location (token->location, token->location, finish_loc);
15632 token->location = combined_loc;
15633
15634 /* Retrieve any deferred checks. Do not pop this access checks yet
15635 so the memory will not be reclaimed during token replacing below. */
15636 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15637 token->u.tree_check_value->value = template_id;
15638 token->u.tree_check_value->checks = get_deferred_access_checks ();
15639 token->keyword = RID_MAX;
15640
15641 /* Purge all subsequent tokens. */
15642 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15643
15644 /* ??? Can we actually assume that, if template_id ==
15645 error_mark_node, we will have issued a diagnostic to the
15646 user, as opposed to simply marking the tentative parse as
15647 failed? */
15648 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15649 error_at (token->location, "parse error in template argument list");
15650 }
15651
15652 pop_to_parent_deferring_access_checks ();
15653 return template_id;
15654}
15655
15656/* Parse a template-name.
15657
15658 template-name:
15659 identifier
15660
15661 The standard should actually say:
15662
15663 template-name:
15664 identifier
15665 operator-function-id
15666
15667 A defect report has been filed about this issue.
15668
15669 A conversion-function-id cannot be a template name because they cannot
15670 be part of a template-id. In fact, looking at this code:
15671
15672 a.operator K<int>()
15673
15674 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15675 It is impossible to call a templated conversion-function-id with an
15676 explicit argument list, since the only allowed template parameter is
15677 the type to which it is converting.
15678
15679 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15680 `template' keyword, in a construction like:
15681
15682 T::template f<3>()
15683
15684 In that case `f' is taken to be a template-name, even though there
15685 is no way of knowing for sure.
15686
15687 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15688 name refers to a set of overloaded functions, at least one of which
15689 is a template, or an IDENTIFIER_NODE with the name of the template,
15690 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15691 names are looked up inside uninstantiated templates. */
15692
15693static tree
15694cp_parser_template_name (cp_parser* parser,
15695 bool template_keyword_p,
15696 bool check_dependency_p,
15697 bool is_declaration,
15698 enum tag_types tag_type,
15699 bool *is_identifier)
15700{
15701 tree identifier;
15702 tree decl;
15703 tree fns;
15704 cp_token *token = cp_lexer_peek_token (parser->lexer);
15705
15706 /* If the next token is `operator', then we have either an
15707 operator-function-id or a conversion-function-id. */
15708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15709 {
15710 /* We don't know whether we're looking at an
15711 operator-function-id or a conversion-function-id. */
15712 cp_parser_parse_tentatively (parser);
15713 /* Try an operator-function-id. */
15714 identifier = cp_parser_operator_function_id (parser);
15715 /* If that didn't work, try a conversion-function-id. */
15716 if (!cp_parser_parse_definitely (parser))
15717 {
15718 cp_parser_error (parser, "expected template-name");
15719 return error_mark_node;
15720 }
15721 }
15722 /* Look for the identifier. */
15723 else
15724 identifier = cp_parser_identifier (parser);
15725
15726 /* If we didn't find an identifier, we don't have a template-id. */
15727 if (identifier == error_mark_node)
15728 return error_mark_node;
15729
15730 /* If the name immediately followed the `template' keyword, then it
15731 is a template-name. However, if the next token is not `<', then
15732 we do not treat it as a template-name, since it is not being used
15733 as part of a template-id. This enables us to handle constructs
15734 like:
15735
15736 template <typename T> struct S { S(); };
15737 template <typename T> S<T>::S();
15738
15739 correctly. We would treat `S' as a template -- if it were `S<T>'
15740 -- but we do not if there is no `<'. */
15741
15742 if (processing_template_decl
15743 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15744 {
15745 /* In a declaration, in a dependent context, we pretend that the
15746 "template" keyword was present in order to improve error
15747 recovery. For example, given:
15748
15749 template <typename T> void f(T::X<int>);
15750
15751 we want to treat "X<int>" as a template-id. */
15752 if (is_declaration
15753 && !template_keyword_p
15754 && parser->scope && TYPE_P (parser->scope)
15755 && check_dependency_p
15756 && dependent_scope_p (parser->scope)
15757 /* Do not do this for dtors (or ctors), since they never
15758 need the template keyword before their name. */
15759 && !constructor_name_p (identifier, parser->scope))
15760 {
15761 cp_token_position start = 0;
15762
15763 /* Explain what went wrong. */
15764 error_at (token->location, "non-template %qD used as template",
15765 identifier);
15766 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15767 parser->scope, identifier);
15768 /* If parsing tentatively, find the location of the "<" token. */
15769 if (cp_parser_simulate_error (parser))
15770 start = cp_lexer_token_position (parser->lexer, true);
15771 /* Parse the template arguments so that we can issue error
15772 messages about them. */
15773 cp_lexer_consume_token (parser->lexer);
15774 cp_parser_enclosed_template_argument_list (parser);
15775 /* Skip tokens until we find a good place from which to
15776 continue parsing. */
15777 cp_parser_skip_to_closing_parenthesis (parser,
15778 /*recovering=*/true,
15779 /*or_comma=*/true,
15780 /*consume_paren=*/false);
15781 /* If parsing tentatively, permanently remove the
15782 template argument list. That will prevent duplicate
15783 error messages from being issued about the missing
15784 "template" keyword. */
15785 if (start)
15786 cp_lexer_purge_tokens_after (parser->lexer, start);
15787 if (is_identifier)
15788 *is_identifier = true;
15789 parser->context->object_type = NULL_TREE;
15790 return identifier;
15791 }
15792
15793 /* If the "template" keyword is present, then there is generally
15794 no point in doing name-lookup, so we just return IDENTIFIER.
15795 But, if the qualifying scope is non-dependent then we can
15796 (and must) do name-lookup normally. */
15797 if (template_keyword_p)
15798 {
15799 tree scope = (parser->scope ? parser->scope
15800 : parser->context->object_type);
15801 if (scope && TYPE_P (scope)
15802 && (!CLASS_TYPE_P (scope)
15803 || (check_dependency_p && dependent_type_p (scope))))
15804 {
15805 /* We're optimizing away the call to cp_parser_lookup_name, but
15806 we still need to do this. */
15807 parser->context->object_type = NULL_TREE;
15808 return identifier;
15809 }
15810 }
15811 }
15812
15813 /* Look up the name. */
15814 decl = cp_parser_lookup_name (parser, identifier,
15815 tag_type,
15816 /*is_template=*/true,
15817 /*is_namespace=*/false,
15818 check_dependency_p,
15819 /*ambiguous_decls=*/NULL,
15820 token->location);
15821
15822 decl = strip_using_decl (decl);
15823
15824 /* If DECL is a template, then the name was a template-name. */
15825 if (TREE_CODE (decl) == TEMPLATE_DECL)
15826 {
15827 if (TREE_DEPRECATED (decl)
15828 && deprecated_state != DEPRECATED_SUPPRESS)
15829 warn_deprecated_use (decl, NULL_TREE);
15830 }
15831 else
15832 {
15833 tree fn = NULL_TREE;
15834
15835 /* The standard does not explicitly indicate whether a name that
15836 names a set of overloaded declarations, some of which are
15837 templates, is a template-name. However, such a name should
15838 be a template-name; otherwise, there is no way to form a
15839 template-id for the overloaded templates. */
15840 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15841 if (TREE_CODE (fns) == OVERLOAD)
15842 for (fn = fns; fn; fn = OVL_NEXT (fn))
15843 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15844 break;
15845
15846 if (!fn)
15847 {
15848 /* The name does not name a template. */
15849 cp_parser_error (parser, "expected template-name");
15850 return error_mark_node;
15851 }
15852 }
15853
15854 /* If DECL is dependent, and refers to a function, then just return
15855 its name; we will look it up again during template instantiation. */
15856 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15857 {
15858 tree scope = ovl_scope (decl);
15859 if (TYPE_P (scope) && dependent_type_p (scope))
15860 return identifier;
15861 }
15862
15863 return decl;
15864}
15865
15866/* Parse a template-argument-list.
15867
15868 template-argument-list:
15869 template-argument ... [opt]
15870 template-argument-list , template-argument ... [opt]
15871
15872 Returns a TREE_VEC containing the arguments. */
15873
15874static tree
15875cp_parser_template_argument_list (cp_parser* parser)
15876{
15877 tree fixed_args[10];
15878 unsigned n_args = 0;
15879 unsigned alloced = 10;
15880 tree *arg_ary = fixed_args;
15881 tree vec;
15882 bool saved_in_template_argument_list_p;
15883 bool saved_ice_p;
15884 bool saved_non_ice_p;
15885
15886 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15887 parser->in_template_argument_list_p = true;
15888 /* Even if the template-id appears in an integral
15889 constant-expression, the contents of the argument list do
15890 not. */
15891 saved_ice_p = parser->integral_constant_expression_p;
15892 parser->integral_constant_expression_p = false;
15893 saved_non_ice_p = parser->non_integral_constant_expression_p;
15894 parser->non_integral_constant_expression_p = false;
15895
15896 /* Parse the arguments. */
15897 do
15898 {
15899 tree argument;
15900
15901 if (n_args)
15902 /* Consume the comma. */
15903 cp_lexer_consume_token (parser->lexer);
15904
15905 /* Parse the template-argument. */
15906 argument = cp_parser_template_argument (parser);
15907
15908 /* If the next token is an ellipsis, we're expanding a template
15909 argument pack. */
15910 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15911 {
15912 if (argument == error_mark_node)
15913 {
15914 cp_token *token = cp_lexer_peek_token (parser->lexer);
15915 error_at (token->location,
15916 "expected parameter pack before %<...%>");
15917 }
15918 /* Consume the `...' token. */
15919 cp_lexer_consume_token (parser->lexer);
15920
15921 /* Make the argument into a TYPE_PACK_EXPANSION or
15922 EXPR_PACK_EXPANSION. */
15923 argument = make_pack_expansion (argument);
15924 }
15925
15926 if (n_args == alloced)
15927 {
15928 alloced *= 2;
15929
15930 if (arg_ary == fixed_args)
15931 {
15932 arg_ary = XNEWVEC (tree, alloced);
15933 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15934 }
15935 else
15936 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15937 }
15938 arg_ary[n_args++] = argument;
15939 }
15940 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15941
15942 vec = make_tree_vec (n_args);
15943
15944 while (n_args--)
15945 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15946
15947 if (arg_ary != fixed_args)
15948 free (arg_ary);
15949 parser->non_integral_constant_expression_p = saved_non_ice_p;
15950 parser->integral_constant_expression_p = saved_ice_p;
15951 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15952 if (CHECKING_P)
15953 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15954 return vec;
15955}
15956
15957/* Parse a template-argument.
15958
15959 template-argument:
15960 assignment-expression
15961 type-id
15962 id-expression
15963
15964 The representation is that of an assignment-expression, type-id, or
15965 id-expression -- except that the qualified id-expression is
15966 evaluated, so that the value returned is either a DECL or an
15967 OVERLOAD.
15968
15969 Although the standard says "assignment-expression", it forbids
15970 throw-expressions or assignments in the template argument.
15971 Therefore, we use "conditional-expression" instead. */
15972
15973static tree
15974cp_parser_template_argument (cp_parser* parser)
15975{
15976 tree argument;
15977 bool template_p;
15978 bool address_p;
15979 bool maybe_type_id = false;
15980 cp_token *token = NULL, *argument_start_token = NULL;
15981 location_t loc = 0;
15982 cp_id_kind idk;
15983
15984 /* There's really no way to know what we're looking at, so we just
15985 try each alternative in order.
15986
15987 [temp.arg]
15988
15989 In a template-argument, an ambiguity between a type-id and an
15990 expression is resolved to a type-id, regardless of the form of
15991 the corresponding template-parameter.
15992
15993 Therefore, we try a type-id first. */
15994 cp_parser_parse_tentatively (parser);
15995 argument = cp_parser_template_type_arg (parser);
15996 /* If there was no error parsing the type-id but the next token is a
15997 '>>', our behavior depends on which dialect of C++ we're
15998 parsing. In C++98, we probably found a typo for '> >'. But there
15999 are type-id which are also valid expressions. For instance:
16000
16001 struct X { int operator >> (int); };
16002 template <int V> struct Foo {};
16003 Foo<X () >> 5> r;
16004
16005 Here 'X()' is a valid type-id of a function type, but the user just
16006 wanted to write the expression "X() >> 5". Thus, we remember that we
16007 found a valid type-id, but we still try to parse the argument as an
16008 expression to see what happens.
16009
16010 In C++0x, the '>>' will be considered two separate '>'
16011 tokens. */
16012 if (!cp_parser_error_occurred (parser)
16013 && cxx_dialect == cxx98
16014 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16015 {
16016 maybe_type_id = true;
16017 cp_parser_abort_tentative_parse (parser);
16018 }
16019 else
16020 {
16021 /* If the next token isn't a `,' or a `>', then this argument wasn't
16022 really finished. This means that the argument is not a valid
16023 type-id. */
16024 if (!cp_parser_next_token_ends_template_argument_p (parser))
16025 cp_parser_error (parser, "expected template-argument");
16026 /* If that worked, we're done. */
16027 if (cp_parser_parse_definitely (parser))
16028 return argument;
16029 }
16030 /* We're still not sure what the argument will be. */
16031 cp_parser_parse_tentatively (parser);
16032 /* Try a template. */
16033 argument_start_token = cp_lexer_peek_token (parser->lexer);
16034 argument = cp_parser_id_expression (parser,
16035 /*template_keyword_p=*/false,
16036 /*check_dependency_p=*/true,
16037 &template_p,
16038 /*declarator_p=*/false,
16039 /*optional_p=*/false);
16040 /* If the next token isn't a `,' or a `>', then this argument wasn't
16041 really finished. */
16042 if (!cp_parser_next_token_ends_template_argument_p (parser))
16043 cp_parser_error (parser, "expected template-argument");
16044 if (!cp_parser_error_occurred (parser))
16045 {
16046 /* Figure out what is being referred to. If the id-expression
16047 was for a class template specialization, then we will have a
16048 TYPE_DECL at this point. There is no need to do name lookup
16049 at this point in that case. */
16050 if (TREE_CODE (argument) != TYPE_DECL)
16051 argument = cp_parser_lookup_name (parser, argument,
16052 none_type,
16053 /*is_template=*/template_p,
16054 /*is_namespace=*/false,
16055 /*check_dependency=*/true,
16056 /*ambiguous_decls=*/NULL,
16057 argument_start_token->location);
16058 /* Handle a constrained-type-specifier for a non-type template
16059 parameter. */
16060 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16061 argument = decl;
16062 else if (TREE_CODE (argument) != TEMPLATE_DECL
16063 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16064 cp_parser_error (parser, "expected template-name");
16065 }
16066 if (cp_parser_parse_definitely (parser))
16067 {
16068 if (TREE_DEPRECATED (argument))
16069 warn_deprecated_use (argument, NULL_TREE);
16070 return argument;
16071 }
16072 /* It must be a non-type argument. In C++17 any constant-expression is
16073 allowed. */
16074 if (cxx_dialect > cxx14)
16075 goto general_expr;
16076
16077 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16078
16079 -- an integral constant-expression of integral or enumeration
16080 type; or
16081
16082 -- the name of a non-type template-parameter; or
16083
16084 -- the name of an object or function with external linkage...
16085
16086 -- the address of an object or function with external linkage...
16087
16088 -- a pointer to member... */
16089 /* Look for a non-type template parameter. */
16090 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16091 {
16092 cp_parser_parse_tentatively (parser);
16093 argument = cp_parser_primary_expression (parser,
16094 /*address_p=*/false,
16095 /*cast_p=*/false,
16096 /*template_arg_p=*/true,
16097 &idk);
16098 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16099 || !cp_parser_next_token_ends_template_argument_p (parser))
16100 cp_parser_simulate_error (parser);
16101 if (cp_parser_parse_definitely (parser))
16102 return argument;
16103 }
16104
16105 /* If the next token is "&", the argument must be the address of an
16106 object or function with external linkage. */
16107 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16108 if (address_p)
16109 {
16110 loc = cp_lexer_peek_token (parser->lexer)->location;
16111 cp_lexer_consume_token (parser->lexer);
16112 }
16113 /* See if we might have an id-expression. */
16114 token = cp_lexer_peek_token (parser->lexer);
16115 if (token->type == CPP_NAME
16116 || token->keyword == RID_OPERATOR
16117 || token->type == CPP_SCOPE
16118 || token->type == CPP_TEMPLATE_ID
16119 || token->type == CPP_NESTED_NAME_SPECIFIER)
16120 {
16121 cp_parser_parse_tentatively (parser);
16122 argument = cp_parser_primary_expression (parser,
16123 address_p,
16124 /*cast_p=*/false,
16125 /*template_arg_p=*/true,
16126 &idk);
16127 if (cp_parser_error_occurred (parser)
16128 || !cp_parser_next_token_ends_template_argument_p (parser))
16129 cp_parser_abort_tentative_parse (parser);
16130 else
16131 {
16132 tree probe;
16133
16134 if (INDIRECT_REF_P (argument))
16135 {
16136 /* Strip the dereference temporarily. */
16137 gcc_assert (REFERENCE_REF_P (argument));
16138 argument = TREE_OPERAND (argument, 0);
16139 }
16140
16141 /* If we're in a template, we represent a qualified-id referring
16142 to a static data member as a SCOPE_REF even if the scope isn't
16143 dependent so that we can check access control later. */
16144 probe = argument;
16145 if (TREE_CODE (probe) == SCOPE_REF)
16146 probe = TREE_OPERAND (probe, 1);
16147 if (VAR_P (probe))
16148 {
16149 /* A variable without external linkage might still be a
16150 valid constant-expression, so no error is issued here
16151 if the external-linkage check fails. */
16152 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16153 cp_parser_simulate_error (parser);
16154 }
16155 else if (is_overloaded_fn (argument))
16156 /* All overloaded functions are allowed; if the external
16157 linkage test does not pass, an error will be issued
16158 later. */
16159 ;
16160 else if (address_p
16161 && (TREE_CODE (argument) == OFFSET_REF
16162 || TREE_CODE (argument) == SCOPE_REF))
16163 /* A pointer-to-member. */
16164 ;
16165 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16166 ;
16167 else
16168 cp_parser_simulate_error (parser);
16169
16170 if (cp_parser_parse_definitely (parser))
16171 {
16172 if (address_p)
16173 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16174 tf_warning_or_error);
16175 else
16176 argument = convert_from_reference (argument);
16177 return argument;
16178 }
16179 }
16180 }
16181 /* If the argument started with "&", there are no other valid
16182 alternatives at this point. */
16183 if (address_p)
16184 {
16185 cp_parser_error (parser, "invalid non-type template argument");
16186 return error_mark_node;
16187 }
16188
16189 general_expr:
16190 /* If the argument wasn't successfully parsed as a type-id followed
16191 by '>>', the argument can only be a constant expression now.
16192 Otherwise, we try parsing the constant-expression tentatively,
16193 because the argument could really be a type-id. */
16194 if (maybe_type_id)
16195 cp_parser_parse_tentatively (parser);
16196
16197 if (cxx_dialect <= cxx14)
16198 argument = cp_parser_constant_expression (parser);
16199 else
16200 {
16201 /* With C++17 generalized non-type template arguments we need to handle
16202 lvalue constant expressions, too. */
16203 argument = cp_parser_assignment_expression (parser);
16204 require_potential_constant_expression (argument);
16205 }
16206
16207 if (!maybe_type_id)
16208 return argument;
16209 if (!cp_parser_next_token_ends_template_argument_p (parser))
16210 cp_parser_error (parser, "expected template-argument");
16211 if (cp_parser_parse_definitely (parser))
16212 return argument;
16213 /* We did our best to parse the argument as a non type-id, but that
16214 was the only alternative that matched (albeit with a '>' after
16215 it). We can assume it's just a typo from the user, and a
16216 diagnostic will then be issued. */
16217 return cp_parser_template_type_arg (parser);
16218}
16219
16220/* Parse an explicit-instantiation.
16221
16222 explicit-instantiation:
16223 template declaration
16224
16225 Although the standard says `declaration', what it really means is:
16226
16227 explicit-instantiation:
16228 template decl-specifier-seq [opt] declarator [opt] ;
16229
16230 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16231 supposed to be allowed. A defect report has been filed about this
16232 issue.
16233
16234 GNU Extension:
16235
16236 explicit-instantiation:
16237 storage-class-specifier template
16238 decl-specifier-seq [opt] declarator [opt] ;
16239 function-specifier template
16240 decl-specifier-seq [opt] declarator [opt] ; */
16241
16242static void
16243cp_parser_explicit_instantiation (cp_parser* parser)
16244{
16245 int declares_class_or_enum;
16246 cp_decl_specifier_seq decl_specifiers;
16247 tree extension_specifier = NULL_TREE;
16248
16249 timevar_push (TV_TEMPLATE_INST);
16250
16251 /* Look for an (optional) storage-class-specifier or
16252 function-specifier. */
16253 if (cp_parser_allow_gnu_extensions_p (parser))
16254 {
16255 extension_specifier
16256 = cp_parser_storage_class_specifier_opt (parser);
16257 if (!extension_specifier)
16258 extension_specifier
16259 = cp_parser_function_specifier_opt (parser,
16260 /*decl_specs=*/NULL);
16261 }
16262
16263 /* Look for the `template' keyword. */
16264 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16265 /* Let the front end know that we are processing an explicit
16266 instantiation. */
16267 begin_explicit_instantiation ();
16268 /* [temp.explicit] says that we are supposed to ignore access
16269 control while processing explicit instantiation directives. */
16270 push_deferring_access_checks (dk_no_check);
16271 /* Parse a decl-specifier-seq. */
16272 cp_parser_decl_specifier_seq (parser,
16273 CP_PARSER_FLAGS_OPTIONAL,
16274 &decl_specifiers,
16275 &declares_class_or_enum);
16276 /* If there was exactly one decl-specifier, and it declared a class,
16277 and there's no declarator, then we have an explicit type
16278 instantiation. */
16279 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16280 {
16281 tree type;
16282
16283 type = check_tag_decl (&decl_specifiers,
16284 /*explicit_type_instantiation_p=*/true);
16285 /* Turn access control back on for names used during
16286 template instantiation. */
16287 pop_deferring_access_checks ();
16288 if (type)
16289 do_type_instantiation (type, extension_specifier,
16290 /*complain=*/tf_error);
16291 }
16292 else
16293 {
16294 cp_declarator *declarator;
16295 tree decl;
16296
16297 /* Parse the declarator. */
16298 declarator
16299 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16300 /*ctor_dtor_or_conv_p=*/NULL,
16301 /*parenthesized_p=*/NULL,
16302 /*member_p=*/false,
16303 /*friend_p=*/false);
16304 if (declares_class_or_enum & 2)
16305 cp_parser_check_for_definition_in_return_type (declarator,
16306 decl_specifiers.type,
16307 decl_specifiers.locations[ds_type_spec]);
16308 if (declarator != cp_error_declarator)
16309 {
16310 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16311 permerror (decl_specifiers.locations[ds_inline],
16312 "explicit instantiation shall not use"
16313 " %<inline%> specifier");
16314 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16315 permerror (decl_specifiers.locations[ds_constexpr],
16316 "explicit instantiation shall not use"
16317 " %<constexpr%> specifier");
16318
16319 decl = grokdeclarator (declarator, &decl_specifiers,
16320 NORMAL, 0, &decl_specifiers.attributes);
16321 /* Turn access control back on for names used during
16322 template instantiation. */
16323 pop_deferring_access_checks ();
16324 /* Do the explicit instantiation. */
16325 do_decl_instantiation (decl, extension_specifier);
16326 }
16327 else
16328 {
16329 pop_deferring_access_checks ();
16330 /* Skip the body of the explicit instantiation. */
16331 cp_parser_skip_to_end_of_statement (parser);
16332 }
16333 }
16334 /* We're done with the instantiation. */
16335 end_explicit_instantiation ();
16336
16337 cp_parser_consume_semicolon_at_end_of_statement (parser);
16338
16339 timevar_pop (TV_TEMPLATE_INST);
16340}
16341
16342/* Parse an explicit-specialization.
16343
16344 explicit-specialization:
16345 template < > declaration
16346
16347 Although the standard says `declaration', what it really means is:
16348
16349 explicit-specialization:
16350 template <> decl-specifier [opt] init-declarator [opt] ;
16351 template <> function-definition
16352 template <> explicit-specialization
16353 template <> template-declaration */
16354
16355static void
16356cp_parser_explicit_specialization (cp_parser* parser)
16357{
16358 bool need_lang_pop;
16359 cp_token *token = cp_lexer_peek_token (parser->lexer);
16360
16361 /* Look for the `template' keyword. */
16362 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16363 /* Look for the `<'. */
16364 cp_parser_require (parser, CPP_LESS, RT_LESS);
16365 /* Look for the `>'. */
16366 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16367 /* We have processed another parameter list. */
16368 ++parser->num_template_parameter_lists;
16369 /* [temp]
16370
16371 A template ... explicit specialization ... shall not have C
16372 linkage. */
16373 if (current_lang_name == lang_name_c)
16374 {
16375 error_at (token->location, "template specialization with C linkage");
16376 /* Give it C++ linkage to avoid confusing other parts of the
16377 front end. */
16378 push_lang_context (lang_name_cplusplus);
16379 need_lang_pop = true;
16380 }
16381 else
16382 need_lang_pop = false;
16383 /* Let the front end know that we are beginning a specialization. */
16384 if (!begin_specialization ())
16385 {
16386 end_specialization ();
16387 return;
16388 }
16389
16390 /* If the next keyword is `template', we need to figure out whether
16391 or not we're looking a template-declaration. */
16392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16393 {
16394 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16395 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16396 cp_parser_template_declaration_after_export (parser,
16397 /*member_p=*/false);
16398 else
16399 cp_parser_explicit_specialization (parser);
16400 }
16401 else
16402 /* Parse the dependent declaration. */
16403 cp_parser_single_declaration (parser,
16404 /*checks=*/NULL,
16405 /*member_p=*/false,
16406 /*explicit_specialization_p=*/true,
16407 /*friend_p=*/NULL);
16408 /* We're done with the specialization. */
16409 end_specialization ();
16410 /* For the erroneous case of a template with C linkage, we pushed an
16411 implicit C++ linkage scope; exit that scope now. */
16412 if (need_lang_pop)
16413 pop_lang_context ();
16414 /* We're done with this parameter list. */
16415 --parser->num_template_parameter_lists;
16416}
16417
16418/* Parse a type-specifier.
16419
16420 type-specifier:
16421 simple-type-specifier
16422 class-specifier
16423 enum-specifier
16424 elaborated-type-specifier
16425 cv-qualifier
16426
16427 GNU Extension:
16428
16429 type-specifier:
16430 __complex__
16431
16432 Returns a representation of the type-specifier. For a
16433 class-specifier, enum-specifier, or elaborated-type-specifier, a
16434 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16435
16436 The parser flags FLAGS is used to control type-specifier parsing.
16437
16438 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16439 in a decl-specifier-seq.
16440
16441 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16442 class-specifier, enum-specifier, or elaborated-type-specifier, then
16443 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16444 if a type is declared; 2 if it is defined. Otherwise, it is set to
16445 zero.
16446
16447 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16448 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16449 is set to FALSE. */
16450
16451static tree
16452cp_parser_type_specifier (cp_parser* parser,
16453 cp_parser_flags flags,
16454 cp_decl_specifier_seq *decl_specs,
16455 bool is_declaration,
16456 int* declares_class_or_enum,
16457 bool* is_cv_qualifier)
16458{
16459 tree type_spec = NULL_TREE;
16460 cp_token *token;
16461 enum rid keyword;
16462 cp_decl_spec ds = ds_last;
16463
16464 /* Assume this type-specifier does not declare a new type. */
16465 if (declares_class_or_enum)
16466 *declares_class_or_enum = 0;
16467 /* And that it does not specify a cv-qualifier. */
16468 if (is_cv_qualifier)
16469 *is_cv_qualifier = false;
16470 /* Peek at the next token. */
16471 token = cp_lexer_peek_token (parser->lexer);
16472
16473 /* If we're looking at a keyword, we can use that to guide the
16474 production we choose. */
16475 keyword = token->keyword;
16476 switch (keyword)
16477 {
16478 case RID_ENUM:
16479 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16480 goto elaborated_type_specifier;
16481
16482 /* Look for the enum-specifier. */
16483 type_spec = cp_parser_enum_specifier (parser);
16484 /* If that worked, we're done. */
16485 if (type_spec)
16486 {
16487 if (declares_class_or_enum)
16488 *declares_class_or_enum = 2;
16489 if (decl_specs)
16490 cp_parser_set_decl_spec_type (decl_specs,
16491 type_spec,
16492 token,
16493 /*type_definition_p=*/true);
16494 return type_spec;
16495 }
16496 else
16497 goto elaborated_type_specifier;
16498
16499 /* Any of these indicate either a class-specifier, or an
16500 elaborated-type-specifier. */
16501 case RID_CLASS:
16502 case RID_STRUCT:
16503 case RID_UNION:
16504 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16505 goto elaborated_type_specifier;
16506
16507 /* Parse tentatively so that we can back up if we don't find a
16508 class-specifier. */
16509 cp_parser_parse_tentatively (parser);
16510 /* Look for the class-specifier. */
16511 type_spec = cp_parser_class_specifier (parser);
16512 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16513 /* If that worked, we're done. */
16514 if (cp_parser_parse_definitely (parser))
16515 {
16516 if (declares_class_or_enum)
16517 *declares_class_or_enum = 2;
16518 if (decl_specs)
16519 cp_parser_set_decl_spec_type (decl_specs,
16520 type_spec,
16521 token,
16522 /*type_definition_p=*/true);
16523 return type_spec;
16524 }
16525
16526 /* Fall through. */
16527 elaborated_type_specifier:
16528 /* We're declaring (not defining) a class or enum. */
16529 if (declares_class_or_enum)
16530 *declares_class_or_enum = 1;
16531
16532 /* Fall through. */
16533 case RID_TYPENAME:
16534 /* Look for an elaborated-type-specifier. */
16535 type_spec
16536 = (cp_parser_elaborated_type_specifier
16537 (parser,
16538 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16539 is_declaration));
16540 if (decl_specs)
16541 cp_parser_set_decl_spec_type (decl_specs,
16542 type_spec,
16543 token,
16544 /*type_definition_p=*/false);
16545 return type_spec;
16546
16547 case RID_CONST:
16548 ds = ds_const;
16549 if (is_cv_qualifier)
16550 *is_cv_qualifier = true;
16551 break;
16552
16553 case RID_VOLATILE:
16554 ds = ds_volatile;
16555 if (is_cv_qualifier)
16556 *is_cv_qualifier = true;
16557 break;
16558
16559 case RID_RESTRICT:
16560 ds = ds_restrict;
16561 if (is_cv_qualifier)
16562 *is_cv_qualifier = true;
16563 break;
16564
16565 case RID_COMPLEX:
16566 /* The `__complex__' keyword is a GNU extension. */
16567 ds = ds_complex;
16568 break;
16569
16570 default:
16571 break;
16572 }
16573
16574 /* Handle simple keywords. */
16575 if (ds != ds_last)
16576 {
16577 if (decl_specs)
16578 {
16579 set_and_check_decl_spec_loc (decl_specs, ds, token);
16580 decl_specs->any_specifiers_p = true;
16581 }
16582 return cp_lexer_consume_token (parser->lexer)->u.value;
16583 }
16584
16585 /* If we do not already have a type-specifier, assume we are looking
16586 at a simple-type-specifier. */
16587 type_spec = cp_parser_simple_type_specifier (parser,
16588 decl_specs,
16589 flags);
16590
16591 /* If we didn't find a type-specifier, and a type-specifier was not
16592 optional in this context, issue an error message. */
16593 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16594 {
16595 cp_parser_error (parser, "expected type specifier");
16596 return error_mark_node;
16597 }
16598
16599 return type_spec;
16600}
16601
16602/* Parse a simple-type-specifier.
16603
16604 simple-type-specifier:
16605 :: [opt] nested-name-specifier [opt] type-name
16606 :: [opt] nested-name-specifier template template-id
16607 char
16608 wchar_t
16609 bool
16610 short
16611 int
16612 long
16613 signed
16614 unsigned
16615 float
16616 double
16617 void
16618
16619 C++11 Extension:
16620
16621 simple-type-specifier:
16622 auto
16623 decltype ( expression )
16624 char16_t
16625 char32_t
16626 __underlying_type ( type-id )
16627
16628 C++17 extension:
16629
16630 nested-name-specifier(opt) template-name
16631
16632 GNU Extension:
16633
16634 simple-type-specifier:
16635 __int128
16636 __typeof__ unary-expression
16637 __typeof__ ( type-id )
16638 __typeof__ ( type-id ) { initializer-list , [opt] }
16639
16640 Concepts Extension:
16641
16642 simple-type-specifier:
16643 constrained-type-specifier
16644
16645 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16646 appropriately updated. */
16647
16648static tree
16649cp_parser_simple_type_specifier (cp_parser* parser,
16650 cp_decl_specifier_seq *decl_specs,
16651 cp_parser_flags flags)
16652{
16653 tree type = NULL_TREE;
16654 cp_token *token;
16655 int idx;
16656
16657 /* Peek at the next token. */
16658 token = cp_lexer_peek_token (parser->lexer);
16659
16660 /* If we're looking at a keyword, things are easy. */
16661 switch (token->keyword)
16662 {
16663 case RID_CHAR:
16664 if (decl_specs)
16665 decl_specs->explicit_char_p = true;
16666 type = char_type_node;
16667 break;
16668 case RID_CHAR16:
16669 type = char16_type_node;
16670 break;
16671 case RID_CHAR32:
16672 type = char32_type_node;
16673 break;
16674 case RID_WCHAR:
16675 type = wchar_type_node;
16676 break;
16677 case RID_BOOL:
16678 type = boolean_type_node;
16679 break;
16680 case RID_SHORT:
16681 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16682 type = short_integer_type_node;
16683 break;
16684 case RID_INT:
16685 if (decl_specs)
16686 decl_specs->explicit_int_p = true;
16687 type = integer_type_node;
16688 break;
16689 case RID_INT_N_0:
16690 case RID_INT_N_1:
16691 case RID_INT_N_2:
16692 case RID_INT_N_3:
16693 idx = token->keyword - RID_INT_N_0;
16694 if (! int_n_enabled_p [idx])
16695 break;
16696 if (decl_specs)
16697 {
16698 decl_specs->explicit_intN_p = true;
16699 decl_specs->int_n_idx = idx;
16700 }
16701 type = int_n_trees [idx].signed_type;
16702 break;
16703 case RID_LONG:
16704 if (decl_specs)
16705 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16706 type = long_integer_type_node;
16707 break;
16708 case RID_SIGNED:
16709 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16710 type = integer_type_node;
16711 break;
16712 case RID_UNSIGNED:
16713 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16714 type = unsigned_type_node;
16715 break;
16716 case RID_FLOAT:
16717 type = float_type_node;
16718 break;
16719 case RID_DOUBLE:
16720 type = double_type_node;
16721 break;
16722 case RID_VOID:
16723 type = void_type_node;
16724 break;
16725
16726 case RID_AUTO:
16727 maybe_warn_cpp0x (CPP0X_AUTO);
16728 if (parser->auto_is_implicit_function_template_parm_p)
16729 {
16730 /* The 'auto' might be the placeholder return type for a function decl
16731 with trailing return type. */
16732 bool have_trailing_return_fn_decl = false;
16733
16734 cp_parser_parse_tentatively (parser);
16735 cp_lexer_consume_token (parser->lexer);
16736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16737 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16738 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16739 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16740 {
16741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16742 {
16743 cp_lexer_consume_token (parser->lexer);
16744 cp_parser_skip_to_closing_parenthesis (parser,
16745 /*recovering*/false,
16746 /*or_comma*/false,
16747 /*consume_paren*/true);
16748 continue;
16749 }
16750
16751 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16752 {
16753 have_trailing_return_fn_decl = true;
16754 break;
16755 }
16756
16757 cp_lexer_consume_token (parser->lexer);
16758 }
16759 cp_parser_abort_tentative_parse (parser);
16760
16761 if (have_trailing_return_fn_decl)
16762 {
16763 type = make_auto ();
16764 break;
16765 }
16766
16767 if (cxx_dialect >= cxx14)
16768 {
16769 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16770 type = TREE_TYPE (type);
16771 }
16772 else
16773 type = error_mark_node;
16774
16775 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16776 {
16777 if (cxx_dialect < cxx14)
16778 error_at (token->location,
16779 "use of %<auto%> in lambda parameter declaration "
16780 "only available with "
16781 "-std=c++14 or -std=gnu++14");
16782 }
16783 else if (cxx_dialect < cxx14)
16784 error_at (token->location,
16785 "use of %<auto%> in parameter declaration "
16786 "only available with "
16787 "-std=c++14 or -std=gnu++14");
16788 else if (!flag_concepts)
16789 pedwarn (token->location, OPT_Wpedantic,
16790 "ISO C++ forbids use of %<auto%> in parameter "
16791 "declaration");
16792 }
16793 else
16794 type = make_auto ();
16795 break;
16796
16797 case RID_DECLTYPE:
16798 /* Since DR 743, decltype can either be a simple-type-specifier by
16799 itself or begin a nested-name-specifier. Parsing it will replace
16800 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16801 handling below decide what to do. */
16802 cp_parser_decltype (parser);
16803 cp_lexer_set_token_position (parser->lexer, token);
16804 break;
16805
16806 case RID_TYPEOF:
16807 /* Consume the `typeof' token. */
16808 cp_lexer_consume_token (parser->lexer);
16809 /* Parse the operand to `typeof'. */
16810 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16811 /* If it is not already a TYPE, take its type. */
16812 if (!TYPE_P (type))
16813 type = finish_typeof (type);
16814
16815 if (decl_specs)
16816 cp_parser_set_decl_spec_type (decl_specs, type,
16817 token,
16818 /*type_definition_p=*/false);
16819
16820 return type;
16821
16822 case RID_UNDERLYING_TYPE:
16823 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16824 if (decl_specs)
16825 cp_parser_set_decl_spec_type (decl_specs, type,
16826 token,
16827 /*type_definition_p=*/false);
16828
16829 return type;
16830
16831 case RID_BASES:
16832 case RID_DIRECT_BASES:
16833 type = cp_parser_trait_expr (parser, token->keyword);
16834 if (decl_specs)
16835 cp_parser_set_decl_spec_type (decl_specs, type,
16836 token,
16837 /*type_definition_p=*/false);
16838 return type;
16839 default:
16840 break;
16841 }
16842
16843 /* If token is an already-parsed decltype not followed by ::,
16844 it's a simple-type-specifier. */
16845 if (token->type == CPP_DECLTYPE
16846 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16847 {
16848 type = saved_checks_value (token->u.tree_check_value);
16849 if (decl_specs)
16850 {
16851 cp_parser_set_decl_spec_type (decl_specs, type,
16852 token,
16853 /*type_definition_p=*/false);
16854 /* Remember that we are handling a decltype in order to
16855 implement the resolution of DR 1510 when the argument
16856 isn't instantiation dependent. */
16857 decl_specs->decltype_p = true;
16858 }
16859 cp_lexer_consume_token (parser->lexer);
16860 return type;
16861 }
16862
16863 /* If the type-specifier was for a built-in type, we're done. */
16864 if (type)
16865 {
16866 /* Record the type. */
16867 if (decl_specs
16868 && (token->keyword != RID_SIGNED
16869 && token->keyword != RID_UNSIGNED
16870 && token->keyword != RID_SHORT
16871 && token->keyword != RID_LONG))
16872 cp_parser_set_decl_spec_type (decl_specs,
16873 type,
16874 token,
16875 /*type_definition_p=*/false);
16876 if (decl_specs)
16877 decl_specs->any_specifiers_p = true;
16878
16879 /* Consume the token. */
16880 cp_lexer_consume_token (parser->lexer);
16881
16882 if (type == error_mark_node)
16883 return error_mark_node;
16884
16885 /* There is no valid C++ program where a non-template type is
16886 followed by a "<". That usually indicates that the user thought
16887 that the type was a template. */
16888 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16889 token->location);
16890
16891 return TYPE_NAME (type);
16892 }
16893
16894 /* The type-specifier must be a user-defined type. */
16895 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16896 {
16897 bool qualified_p;
16898 bool global_p;
16899
16900 /* Don't gobble tokens or issue error messages if this is an
16901 optional type-specifier. */
16902 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16903 cp_parser_parse_tentatively (parser);
16904
16905 token = cp_lexer_peek_token (parser->lexer);
16906
16907 /* Look for the optional `::' operator. */
16908 global_p
16909 = (cp_parser_global_scope_opt (parser,
16910 /*current_scope_valid_p=*/false)
16911 != NULL_TREE);
16912 /* Look for the nested-name specifier. */
16913 qualified_p
16914 = (cp_parser_nested_name_specifier_opt (parser,
16915 /*typename_keyword_p=*/false,
16916 /*check_dependency_p=*/true,
16917 /*type_p=*/false,
16918 /*is_declaration=*/false)
16919 != NULL_TREE);
16920 /* If we have seen a nested-name-specifier, and the next token
16921 is `template', then we are using the template-id production. */
16922 if (parser->scope
16923 && cp_parser_optional_template_keyword (parser))
16924 {
16925 /* Look for the template-id. */
16926 type = cp_parser_template_id (parser,
16927 /*template_keyword_p=*/true,
16928 /*check_dependency_p=*/true,
16929 none_type,
16930 /*is_declaration=*/false);
16931 /* If the template-id did not name a type, we are out of
16932 luck. */
16933 if (TREE_CODE (type) != TYPE_DECL)
16934 {
16935 cp_parser_error (parser, "expected template-id for type");
16936 type = NULL_TREE;
16937 }
16938 }
16939 /* Otherwise, look for a type-name. */
16940 else
16941 type = cp_parser_type_name (parser);
16942 /* Keep track of all name-lookups performed in class scopes. */
16943 if (type
16944 && !global_p
16945 && !qualified_p
16946 && TREE_CODE (type) == TYPE_DECL
16947 && identifier_p (DECL_NAME (type)))
16948 maybe_note_name_used_in_class (DECL_NAME (type), type);
16949 /* If it didn't work out, we don't have a TYPE. */
16950 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16951 && !cp_parser_parse_definitely (parser))
16952 type = NULL_TREE;
16953 if (!type && cxx_dialect >= cxx1z)
16954 {
16955 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16956 cp_parser_parse_tentatively (parser);
16957
16958 cp_parser_global_scope_opt (parser,
16959 /*current_scope_valid_p=*/false);
16960 cp_parser_nested_name_specifier_opt (parser,
16961 /*typename_keyword_p=*/false,
16962 /*check_dependency_p=*/true,
16963 /*type_p=*/false,
16964 /*is_declaration=*/false);
16965 tree name = cp_parser_identifier (parser);
16966 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16967 && parser->scope != error_mark_node)
16968 {
16969 tree tmpl = cp_parser_lookup_name (parser, name,
16970 none_type,
16971 /*is_template=*/false,
16972 /*is_namespace=*/false,
16973 /*check_dependency=*/true,
16974 /*ambiguous_decls=*/NULL,
16975 token->location);
16976 if (tmpl && tmpl != error_mark_node
16977 && (DECL_CLASS_TEMPLATE_P (tmpl)
16978 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16979 type = make_template_placeholder (tmpl);
16980 else
16981 {
16982 type = error_mark_node;
16983 if (!cp_parser_simulate_error (parser))
16984 cp_parser_name_lookup_error (parser, name, tmpl,
16985 NLE_TYPE, token->location);
16986 }
16987 }
16988 else
16989 type = error_mark_node;
16990
16991 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16992 && !cp_parser_parse_definitely (parser))
16993 type = NULL_TREE;
16994 }
16995 if (type && decl_specs)
16996 cp_parser_set_decl_spec_type (decl_specs, type,
16997 token,
16998 /*type_definition_p=*/false);
16999 }
17000
17001 /* If we didn't get a type-name, issue an error message. */
17002 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17003 {
17004 cp_parser_error (parser, "expected type-name");
17005 return error_mark_node;
17006 }
17007
17008 if (type && type != error_mark_node)
17009 {
17010 /* See if TYPE is an Objective-C type, and if so, parse and
17011 accept any protocol references following it. Do this before
17012 the cp_parser_check_for_invalid_template_id() call, because
17013 Objective-C types can be followed by '<...>' which would
17014 enclose protocol names rather than template arguments, and so
17015 everything is fine. */
17016 if (c_dialect_objc () && !parser->scope
17017 && (objc_is_id (type) || objc_is_class_name (type)))
17018 {
17019 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17020 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17021
17022 /* Clobber the "unqualified" type previously entered into
17023 DECL_SPECS with the new, improved protocol-qualified version. */
17024 if (decl_specs)
17025 decl_specs->type = qual_type;
17026
17027 return qual_type;
17028 }
17029
17030 /* There is no valid C++ program where a non-template type is
17031 followed by a "<". That usually indicates that the user
17032 thought that the type was a template. */
17033 cp_parser_check_for_invalid_template_id (parser, type,
17034 none_type,
17035 token->location);
17036 }
17037
17038 return type;
17039}
17040
17041/* Parse a type-name.
17042
17043 type-name:
17044 class-name
17045 enum-name
17046 typedef-name
17047 simple-template-id [in c++0x]
17048
17049 enum-name:
17050 identifier
17051
17052 typedef-name:
17053 identifier
17054
17055 Concepts:
17056
17057 type-name:
17058 concept-name
17059 partial-concept-id
17060
17061 concept-name:
17062 identifier
17063
17064 Returns a TYPE_DECL for the type. */
17065
17066static tree
17067cp_parser_type_name (cp_parser* parser)
17068{
17069 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17070}
17071
17072/* See above. */
17073static tree
17074cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17075{
17076 tree type_decl;
17077
17078 /* We can't know yet whether it is a class-name or not. */
17079 cp_parser_parse_tentatively (parser);
17080 /* Try a class-name. */
17081 type_decl = cp_parser_class_name (parser,
17082 typename_keyword_p,
17083 /*template_keyword_p=*/false,
17084 none_type,
17085 /*check_dependency_p=*/true,
17086 /*class_head_p=*/false,
17087 /*is_declaration=*/false);
17088 /* If it's not a class-name, keep looking. */
17089 if (!cp_parser_parse_definitely (parser))
17090 {
17091 if (cxx_dialect < cxx11)
17092 /* It must be a typedef-name or an enum-name. */
17093 return cp_parser_nonclass_name (parser);
17094
17095 cp_parser_parse_tentatively (parser);
17096 /* It is either a simple-template-id representing an
17097 instantiation of an alias template... */
17098 type_decl = cp_parser_template_id (parser,
17099 /*template_keyword_p=*/false,
17100 /*check_dependency_p=*/true,
17101 none_type,
17102 /*is_declaration=*/false);
17103 /* Note that this must be an instantiation of an alias template
17104 because [temp.names]/6 says:
17105
17106 A template-id that names an alias template specialization
17107 is a type-name.
17108
17109 Whereas [temp.names]/7 says:
17110
17111 A simple-template-id that names a class template
17112 specialization is a class-name.
17113
17114 With concepts, this could also be a partial-concept-id that
17115 declares a non-type template parameter. */
17116 if (type_decl != NULL_TREE
17117 && TREE_CODE (type_decl) == TYPE_DECL
17118 && TYPE_DECL_ALIAS_P (type_decl))
17119 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17120 else if (is_constrained_parameter (type_decl))
17121 /* Don't do anything. */ ;
17122 else
17123 cp_parser_simulate_error (parser);
17124
17125 if (!cp_parser_parse_definitely (parser))
17126 /* ... Or a typedef-name or an enum-name. */
17127 return cp_parser_nonclass_name (parser);
17128 }
17129
17130 return type_decl;
17131}
17132
17133/* Check if DECL and ARGS can form a constrained-type-specifier.
17134 If ARGS is non-null, we try to form a concept check of the
17135 form DECL<?, ARGS> where ? is a wildcard that matches any
17136 kind of template argument. If ARGS is NULL, then we try to
17137 form a concept check of the form DECL<?>. */
17138
17139static tree
17140cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17141 tree decl, tree args)
17142{
17143 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17144
17145 /* If we a constrained-type-specifier cannot be deduced. */
17146 if (parser->prevent_constrained_type_specifiers)
17147 return NULL_TREE;
17148
17149 /* A constrained type specifier can only be found in an
17150 overload set or as a reference to a template declaration.
17151
17152 FIXME: This might be masking a bug. It's possible that
17153 that the deduction below is causing template specializations
17154 to be formed with the wildcard as an argument. */
17155 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17156 return NULL_TREE;
17157
17158 /* Try to build a call expression that evaluates the
17159 concept. This can fail if the overload set refers
17160 only to non-templates. */
17161 tree placeholder = build_nt (WILDCARD_DECL);
17162 tree check = build_concept_check (decl, placeholder, args);
17163 if (check == error_mark_node)
17164 return NULL_TREE;
17165
17166 /* Deduce the checked constraint and the prototype parameter.
17167
17168 FIXME: In certain cases, failure to deduce should be a
17169 diagnosable error. */
17170 tree conc;
17171 tree proto;
17172 if (!deduce_constrained_parameter (check, conc, proto))
17173 return NULL_TREE;
17174
17175 /* In template parameter scope, this results in a constrained
17176 parameter. Return a descriptor of that parm. */
17177 if (processing_template_parmlist)
17178 return build_constrained_parameter (conc, proto, args);
17179
17180 /* In a parameter-declaration-clause, constrained-type
17181 specifiers result in invented template parameters. */
17182 if (parser->auto_is_implicit_function_template_parm_p)
17183 {
17184 tree x = build_constrained_parameter (conc, proto, args);
17185 return synthesize_implicit_template_parm (parser, x);
17186 }
17187 else
17188 {
17189 /* Otherwise, we're in a context where the constrained
17190 type name is deduced and the constraint applies
17191 after deduction. */
17192 return make_constrained_auto (conc, args);
17193 }
17194
17195 return NULL_TREE;
17196}
17197
17198/* If DECL refers to a concept, return a TYPE_DECL representing
17199 the result of using the constrained type specifier in the
17200 current context. DECL refers to a concept if
17201
17202 - it is an overload set containing a function concept taking a single
17203 type argument, or
17204
17205 - it is a variable concept taking a single type argument. */
17206
17207static tree
17208cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17209{
17210 if (flag_concepts
17211 && (TREE_CODE (decl) == OVERLOAD
17212 || BASELINK_P (decl)
17213 || variable_concept_p (decl)))
17214 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17215 else
17216 return NULL_TREE;
17217}
17218
17219/* Check if DECL and ARGS form a partial-concept-id. If so,
17220 assign ID to the resulting constrained placeholder.
17221
17222 Returns true if the partial-concept-id designates a placeholder
17223 and false otherwise. Note that *id is set to NULL_TREE in
17224 this case. */
17225
17226static tree
17227cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17228{
17229 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17230}
17231
17232/* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17233 or a concept-name.
17234
17235 enum-name:
17236 identifier
17237
17238 typedef-name:
17239 identifier
17240
17241 concept-name:
17242 identifier
17243
17244 Returns a TYPE_DECL for the type. */
17245
17246static tree
17247cp_parser_nonclass_name (cp_parser* parser)
17248{
17249 tree type_decl;
17250 tree identifier;
17251
17252 cp_token *token = cp_lexer_peek_token (parser->lexer);
17253 identifier = cp_parser_identifier (parser);
17254 if (identifier == error_mark_node)
17255 return error_mark_node;
17256
17257 /* Look up the type-name. */
17258 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17259
17260 type_decl = strip_using_decl (type_decl);
17261
17262 /* If we found an overload set, then it may refer to a concept-name. */
17263 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17264 type_decl = decl;
17265
17266 if (TREE_CODE (type_decl) != TYPE_DECL
17267 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17268 {
17269 /* See if this is an Objective-C type. */
17270 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17271 tree type = objc_get_protocol_qualified_type (identifier, protos);
17272 if (type)
17273 type_decl = TYPE_NAME (type);
17274 }
17275
17276 /* Issue an error if we did not find a type-name. */
17277 if (TREE_CODE (type_decl) != TYPE_DECL
17278 /* In Objective-C, we have the complication that class names are
17279 normally type names and start declarations (eg, the
17280 "NSObject" in "NSObject *object;"), but can be used in an
17281 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17282 is an expression. So, a classname followed by a dot is not a
17283 valid type-name. */
17284 || (objc_is_class_name (TREE_TYPE (type_decl))
17285 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17286 {
17287 if (!cp_parser_simulate_error (parser))
17288 cp_parser_name_lookup_error (parser, identifier, type_decl,
17289 NLE_TYPE, token->location);
17290 return error_mark_node;
17291 }
17292 /* Remember that the name was used in the definition of the
17293 current class so that we can check later to see if the
17294 meaning would have been different after the class was
17295 entirely defined. */
17296 else if (type_decl != error_mark_node
17297 && !parser->scope)
17298 maybe_note_name_used_in_class (identifier, type_decl);
17299
17300 return type_decl;
17301}
17302
17303/* Parse an elaborated-type-specifier. Note that the grammar given
17304 here incorporates the resolution to DR68.
17305
17306 elaborated-type-specifier:
17307 class-key :: [opt] nested-name-specifier [opt] identifier
17308 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17309 enum-key :: [opt] nested-name-specifier [opt] identifier
17310 typename :: [opt] nested-name-specifier identifier
17311 typename :: [opt] nested-name-specifier template [opt]
17312 template-id
17313
17314 GNU extension:
17315
17316 elaborated-type-specifier:
17317 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17318 class-key attributes :: [opt] nested-name-specifier [opt]
17319 template [opt] template-id
17320 enum attributes :: [opt] nested-name-specifier [opt] identifier
17321
17322 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17323 declared `friend'. If IS_DECLARATION is TRUE, then this
17324 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17325 something is being declared.
17326
17327 Returns the TYPE specified. */
17328
17329static tree
17330cp_parser_elaborated_type_specifier (cp_parser* parser,
17331 bool is_friend,
17332 bool is_declaration)
17333{
17334 enum tag_types tag_type;
17335 tree identifier;
17336 tree type = NULL_TREE;
17337 tree attributes = NULL_TREE;
17338 tree globalscope;
17339 cp_token *token = NULL;
17340
17341 /* See if we're looking at the `enum' keyword. */
17342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17343 {
17344 /* Consume the `enum' token. */
17345 cp_lexer_consume_token (parser->lexer);
17346 /* Remember that it's an enumeration type. */
17347 tag_type = enum_type;
17348 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17349 enums) is used here. */
17350 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17351 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17352 {
17353 pedwarn (input_location, 0, "elaborated-type-specifier "
17354 "for a scoped enum must not use the %<%D%> keyword",
17355 cp_lexer_peek_token (parser->lexer)->u.value);
17356 /* Consume the `struct' or `class' and parse it anyway. */
17357 cp_lexer_consume_token (parser->lexer);
17358 }
17359 /* Parse the attributes. */
17360 attributes = cp_parser_attributes_opt (parser);
17361 }
17362 /* Or, it might be `typename'. */
17363 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17364 RID_TYPENAME))
17365 {
17366 /* Consume the `typename' token. */
17367 cp_lexer_consume_token (parser->lexer);
17368 /* Remember that it's a `typename' type. */
17369 tag_type = typename_type;
17370 }
17371 /* Otherwise it must be a class-key. */
17372 else
17373 {
17374 tag_type = cp_parser_class_key (parser);
17375 if (tag_type == none_type)
17376 return error_mark_node;
17377 /* Parse the attributes. */
17378 attributes = cp_parser_attributes_opt (parser);
17379 }
17380
17381 /* Look for the `::' operator. */
17382 globalscope = cp_parser_global_scope_opt (parser,
17383 /*current_scope_valid_p=*/false);
17384 /* Look for the nested-name-specifier. */
17385 tree nested_name_specifier;
17386 if (tag_type == typename_type && !globalscope)
17387 {
17388 nested_name_specifier
17389 = cp_parser_nested_name_specifier (parser,
17390 /*typename_keyword_p=*/true,
17391 /*check_dependency_p=*/true,
17392 /*type_p=*/true,
17393 is_declaration);
17394 if (!nested_name_specifier)
17395 return error_mark_node;
17396 }
17397 else
17398 /* Even though `typename' is not present, the proposed resolution
17399 to Core Issue 180 says that in `class A<T>::B', `B' should be
17400 considered a type-name, even if `A<T>' is dependent. */
17401 nested_name_specifier
17402 = cp_parser_nested_name_specifier_opt (parser,
17403 /*typename_keyword_p=*/true,
17404 /*check_dependency_p=*/true,
17405 /*type_p=*/true,
17406 is_declaration);
17407 /* For everything but enumeration types, consider a template-id.
17408 For an enumeration type, consider only a plain identifier. */
17409 if (tag_type != enum_type)
17410 {
17411 bool template_p = false;
17412 tree decl;
17413
17414 /* Allow the `template' keyword. */
17415 template_p = cp_parser_optional_template_keyword (parser);
17416 /* If we didn't see `template', we don't know if there's a
17417 template-id or not. */
17418 if (!template_p)
17419 cp_parser_parse_tentatively (parser);
17420 /* Parse the template-id. */
17421 token = cp_lexer_peek_token (parser->lexer);
17422 decl = cp_parser_template_id (parser, template_p,
17423 /*check_dependency_p=*/true,
17424 tag_type,
17425 is_declaration);
17426 /* If we didn't find a template-id, look for an ordinary
17427 identifier. */
17428 if (!template_p && !cp_parser_parse_definitely (parser))
17429 ;
17430 /* We can get here when cp_parser_template_id, called by
17431 cp_parser_class_name with tag_type == none_type, succeeds
17432 and caches a BASELINK. Then, when called again here,
17433 instead of failing and returning an error_mark_node
17434 returns it (see template/typename17.C in C++11).
17435 ??? Could we diagnose this earlier? */
17436 else if (tag_type == typename_type && BASELINK_P (decl))
17437 {
17438 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17439 type = error_mark_node;
17440 }
17441 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17442 in effect, then we must assume that, upon instantiation, the
17443 template will correspond to a class. */
17444 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17445 && tag_type == typename_type)
17446 type = make_typename_type (parser->scope, decl,
17447 typename_type,
17448 /*complain=*/tf_error);
17449 /* If the `typename' keyword is in effect and DECL is not a type
17450 decl, then type is non existent. */
17451 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17452 ;
17453 else if (TREE_CODE (decl) == TYPE_DECL)
17454 {
17455 type = check_elaborated_type_specifier (tag_type, decl,
17456 /*allow_template_p=*/true);
17457
17458 /* If the next token is a semicolon, this must be a specialization,
17459 instantiation, or friend declaration. Check the scope while we
17460 still know whether or not we had a nested-name-specifier. */
17461 if (type != error_mark_node
17462 && !nested_name_specifier && !is_friend
17463 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17464 check_unqualified_spec_or_inst (type, token->location);
17465 }
17466 else if (decl == error_mark_node)
17467 type = error_mark_node;
17468 }
17469
17470 if (!type)
17471 {
17472 token = cp_lexer_peek_token (parser->lexer);
17473 identifier = cp_parser_identifier (parser);
17474
17475 if (identifier == error_mark_node)
17476 {
17477 parser->scope = NULL_TREE;
17478 return error_mark_node;
17479 }
17480
17481 /* For a `typename', we needn't call xref_tag. */
17482 if (tag_type == typename_type
17483 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17484 return cp_parser_make_typename_type (parser, identifier,
17485 token->location);
17486
17487 /* Template parameter lists apply only if we are not within a
17488 function parameter list. */
17489 bool template_parm_lists_apply
17490 = parser->num_template_parameter_lists;
17491 if (template_parm_lists_apply)
17492 for (cp_binding_level *s = current_binding_level;
17493 s && s->kind != sk_template_parms;
17494 s = s->level_chain)
17495 if (s->kind == sk_function_parms)
17496 template_parm_lists_apply = false;
17497
17498 /* Look up a qualified name in the usual way. */
17499 if (parser->scope)
17500 {
17501 tree decl;
17502 tree ambiguous_decls;
17503
17504 decl = cp_parser_lookup_name (parser, identifier,
17505 tag_type,
17506 /*is_template=*/false,
17507 /*is_namespace=*/false,
17508 /*check_dependency=*/true,
17509 &ambiguous_decls,
17510 token->location);
17511
17512 /* If the lookup was ambiguous, an error will already have been
17513 issued. */
17514 if (ambiguous_decls)
17515 return error_mark_node;
17516
17517 /* If we are parsing friend declaration, DECL may be a
17518 TEMPLATE_DECL tree node here. However, we need to check
17519 whether this TEMPLATE_DECL results in valid code. Consider
17520 the following example:
17521
17522 namespace N {
17523 template <class T> class C {};
17524 }
17525 class X {
17526 template <class T> friend class N::C; // #1, valid code
17527 };
17528 template <class T> class Y {
17529 friend class N::C; // #2, invalid code
17530 };
17531
17532 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17533 name lookup of `N::C'. We see that friend declaration must
17534 be template for the code to be valid. Note that
17535 processing_template_decl does not work here since it is
17536 always 1 for the above two cases. */
17537
17538 decl = (cp_parser_maybe_treat_template_as_class
17539 (decl, /*tag_name_p=*/is_friend
17540 && template_parm_lists_apply));
17541
17542 if (TREE_CODE (decl) != TYPE_DECL)
17543 {
17544 cp_parser_diagnose_invalid_type_name (parser,
17545 identifier,
17546 token->location);
17547 return error_mark_node;
17548 }
17549
17550 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17551 {
17552 bool allow_template = (template_parm_lists_apply
17553 || DECL_SELF_REFERENCE_P (decl));
17554 type = check_elaborated_type_specifier (tag_type, decl,
17555 allow_template);
17556
17557 if (type == error_mark_node)
17558 return error_mark_node;
17559 }
17560
17561 /* Forward declarations of nested types, such as
17562
17563 class C1::C2;
17564 class C1::C2::C3;
17565
17566 are invalid unless all components preceding the final '::'
17567 are complete. If all enclosing types are complete, these
17568 declarations become merely pointless.
17569
17570 Invalid forward declarations of nested types are errors
17571 caught elsewhere in parsing. Those that are pointless arrive
17572 here. */
17573
17574 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17575 && !is_friend && !processing_explicit_instantiation)
17576 warning (0, "declaration %qD does not declare anything", decl);
17577
17578 type = TREE_TYPE (decl);
17579 }
17580 else
17581 {
17582 /* An elaborated-type-specifier sometimes introduces a new type and
17583 sometimes names an existing type. Normally, the rule is that it
17584 introduces a new type only if there is not an existing type of
17585 the same name already in scope. For example, given:
17586
17587 struct S {};
17588 void f() { struct S s; }
17589
17590 the `struct S' in the body of `f' is the same `struct S' as in
17591 the global scope; the existing definition is used. However, if
17592 there were no global declaration, this would introduce a new
17593 local class named `S'.
17594
17595 An exception to this rule applies to the following code:
17596
17597 namespace N { struct S; }
17598
17599 Here, the elaborated-type-specifier names a new type
17600 unconditionally; even if there is already an `S' in the
17601 containing scope this declaration names a new type.
17602 This exception only applies if the elaborated-type-specifier
17603 forms the complete declaration:
17604
17605 [class.name]
17606
17607 A declaration consisting solely of `class-key identifier ;' is
17608 either a redeclaration of the name in the current scope or a
17609 forward declaration of the identifier as a class name. It
17610 introduces the name into the current scope.
17611
17612 We are in this situation precisely when the next token is a `;'.
17613
17614 An exception to the exception is that a `friend' declaration does
17615 *not* name a new type; i.e., given:
17616
17617 struct S { friend struct T; };
17618
17619 `T' is not a new type in the scope of `S'.
17620
17621 Also, `new struct S' or `sizeof (struct S)' never results in the
17622 definition of a new type; a new type can only be declared in a
17623 declaration context. */
17624
17625 tag_scope ts;
17626 bool template_p;
17627
17628 if (is_friend)
17629 /* Friends have special name lookup rules. */
17630 ts = ts_within_enclosing_non_class;
17631 else if (is_declaration
17632 && cp_lexer_next_token_is (parser->lexer,
17633 CPP_SEMICOLON))
17634 /* This is a `class-key identifier ;' */
17635 ts = ts_current;
17636 else
17637 ts = ts_global;
17638
17639 template_p =
17640 (template_parm_lists_apply
17641 && (cp_parser_next_token_starts_class_definition_p (parser)
17642 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17643 /* An unqualified name was used to reference this type, so
17644 there were no qualifying templates. */
17645 if (template_parm_lists_apply
17646 && !cp_parser_check_template_parameters (parser,
17647 /*num_templates=*/0,
17648 token->location,
17649 /*declarator=*/NULL))
17650 return error_mark_node;
17651 type = xref_tag (tag_type, identifier, ts, template_p);
17652 }
17653 }
17654
17655 if (type == error_mark_node)
17656 return error_mark_node;
17657
17658 /* Allow attributes on forward declarations of classes. */
17659 if (attributes)
17660 {
17661 if (TREE_CODE (type) == TYPENAME_TYPE)
17662 warning (OPT_Wattributes,
17663 "attributes ignored on uninstantiated type");
17664 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17665 && ! processing_explicit_instantiation)
17666 warning (OPT_Wattributes,
17667 "attributes ignored on template instantiation");
17668 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17669 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17670 else
17671 warning (OPT_Wattributes,
17672 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17673 }
17674
17675 if (tag_type != enum_type)
17676 {
17677 /* Indicate whether this class was declared as a `class' or as a
17678 `struct'. */
17679 if (CLASS_TYPE_P (type))
17680 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17681 cp_parser_check_class_key (tag_type, type);
17682 }
17683
17684 /* A "<" cannot follow an elaborated type specifier. If that
17685 happens, the user was probably trying to form a template-id. */
17686 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17687 token->location);
17688
17689 return type;
17690}
17691
17692/* Parse an enum-specifier.
17693
17694 enum-specifier:
17695 enum-head { enumerator-list [opt] }
17696 enum-head { enumerator-list , } [C++0x]
17697
17698 enum-head:
17699 enum-key identifier [opt] enum-base [opt]
17700 enum-key nested-name-specifier identifier enum-base [opt]
17701
17702 enum-key:
17703 enum
17704 enum class [C++0x]
17705 enum struct [C++0x]
17706
17707 enum-base: [C++0x]
17708 : type-specifier-seq
17709
17710 opaque-enum-specifier:
17711 enum-key identifier enum-base [opt] ;
17712
17713 GNU Extensions:
17714 enum-key attributes[opt] identifier [opt] enum-base [opt]
17715 { enumerator-list [opt] }attributes[opt]
17716 enum-key attributes[opt] identifier [opt] enum-base [opt]
17717 { enumerator-list, }attributes[opt] [C++0x]
17718
17719 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17720 if the token stream isn't an enum-specifier after all. */
17721
17722static tree
17723cp_parser_enum_specifier (cp_parser* parser)
17724{
17725 tree identifier;
17726 tree type = NULL_TREE;
17727 tree prev_scope;
17728 tree nested_name_specifier = NULL_TREE;
17729 tree attributes;
17730 bool scoped_enum_p = false;
17731 bool has_underlying_type = false;
17732 bool nested_being_defined = false;
17733 bool new_value_list = false;
17734 bool is_new_type = false;
17735 bool is_unnamed = false;
17736 tree underlying_type = NULL_TREE;
17737 cp_token *type_start_token = NULL;
17738 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17739
17740 parser->colon_corrects_to_scope_p = false;
17741
17742 /* Parse tentatively so that we can back up if we don't find a
17743 enum-specifier. */
17744 cp_parser_parse_tentatively (parser);
17745
17746 /* Caller guarantees that the current token is 'enum', an identifier
17747 possibly follows, and the token after that is an opening brace.
17748 If we don't have an identifier, fabricate an anonymous name for
17749 the enumeration being defined. */
17750 cp_lexer_consume_token (parser->lexer);
17751
17752 /* Parse the "class" or "struct", which indicates a scoped
17753 enumeration type in C++0x. */
17754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17755 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17756 {
17757 if (cxx_dialect < cxx11)
17758 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17759
17760 /* Consume the `struct' or `class' token. */
17761 cp_lexer_consume_token (parser->lexer);
17762
17763 scoped_enum_p = true;
17764 }
17765
17766 attributes = cp_parser_attributes_opt (parser);
17767
17768 /* Clear the qualification. */
17769 parser->scope = NULL_TREE;
17770 parser->qualifying_scope = NULL_TREE;
17771 parser->object_scope = NULL_TREE;
17772
17773 /* Figure out in what scope the declaration is being placed. */
17774 prev_scope = current_scope ();
17775
17776 type_start_token = cp_lexer_peek_token (parser->lexer);
17777
17778 push_deferring_access_checks (dk_no_check);
17779 nested_name_specifier
17780 = cp_parser_nested_name_specifier_opt (parser,
17781 /*typename_keyword_p=*/true,
17782 /*check_dependency_p=*/false,
17783 /*type_p=*/false,
17784 /*is_declaration=*/false);
17785
17786 if (nested_name_specifier)
17787 {
17788 tree name;
17789
17790 identifier = cp_parser_identifier (parser);
17791 name = cp_parser_lookup_name (parser, identifier,
17792 enum_type,
17793 /*is_template=*/false,
17794 /*is_namespace=*/false,
17795 /*check_dependency=*/true,
17796 /*ambiguous_decls=*/NULL,
17797 input_location);
17798 if (name && name != error_mark_node)
17799 {
17800 type = TREE_TYPE (name);
17801 if (TREE_CODE (type) == TYPENAME_TYPE)
17802 {
17803 /* Are template enums allowed in ISO? */
17804 if (template_parm_scope_p ())
17805 pedwarn (type_start_token->location, OPT_Wpedantic,
17806 "%qD is an enumeration template", name);
17807 /* ignore a typename reference, for it will be solved by name
17808 in start_enum. */
17809 type = NULL_TREE;
17810 }
17811 }
17812 else if (nested_name_specifier == error_mark_node)
17813 /* We already issued an error. */;
17814 else
17815 {
17816 error_at (type_start_token->location,
17817 "%qD does not name an enumeration in %qT",
17818 identifier, nested_name_specifier);
17819 nested_name_specifier = error_mark_node;
17820 }
17821 }
17822 else
17823 {
17824 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17825 identifier = cp_parser_identifier (parser);
17826 else
17827 {
17828 identifier = make_anon_name ();
17829 is_unnamed = true;
17830 if (scoped_enum_p)
17831 error_at (type_start_token->location,
17832 "unnamed scoped enum is not allowed");
17833 }
17834 }
17835 pop_deferring_access_checks ();
17836
17837 /* Check for the `:' that denotes a specified underlying type in C++0x.
17838 Note that a ':' could also indicate a bitfield width, however. */
17839 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17840 {
17841 cp_decl_specifier_seq type_specifiers;
17842
17843 /* Consume the `:'. */
17844 cp_lexer_consume_token (parser->lexer);
17845
17846 /* Parse the type-specifier-seq. */
17847 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17848 /*is_trailing_return=*/false,
17849 &type_specifiers);
17850
17851 /* At this point this is surely not elaborated type specifier. */
17852 if (!cp_parser_parse_definitely (parser))
17853 return NULL_TREE;
17854
17855 if (cxx_dialect < cxx11)
17856 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17857
17858 has_underlying_type = true;
17859
17860 /* If that didn't work, stop. */
17861 if (type_specifiers.type != error_mark_node)
17862 {
17863 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17864 /*initialized=*/0, NULL);
17865 if (underlying_type == error_mark_node
17866 || check_for_bare_parameter_packs (underlying_type))
17867 underlying_type = NULL_TREE;
17868 }
17869 }
17870
17871 /* Look for the `{' but don't consume it yet. */
17872 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17873 {
17874 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17875 {
17876 cp_parser_error (parser, "expected %<{%>");
17877 if (has_underlying_type)
17878 {
17879 type = NULL_TREE;
17880 goto out;
17881 }
17882 }
17883 /* An opaque-enum-specifier must have a ';' here. */
17884 if ((scoped_enum_p || underlying_type)
17885 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17886 {
17887 cp_parser_error (parser, "expected %<;%> or %<{%>");
17888 if (has_underlying_type)
17889 {
17890 type = NULL_TREE;
17891 goto out;
17892 }
17893 }
17894 }
17895
17896 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17897 return NULL_TREE;
17898
17899 if (nested_name_specifier)
17900 {
17901 if (CLASS_TYPE_P (nested_name_specifier))
17902 {
17903 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17904 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17905 push_scope (nested_name_specifier);
17906 }
17907 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17908 {
17909 push_nested_namespace (nested_name_specifier);
17910 }
17911 }
17912
17913 /* Issue an error message if type-definitions are forbidden here. */
17914 if (!cp_parser_check_type_definition (parser))
17915 type = error_mark_node;
17916 else
17917 /* Create the new type. We do this before consuming the opening
17918 brace so the enum will be recorded as being on the line of its
17919 tag (or the 'enum' keyword, if there is no tag). */
17920 type = start_enum (identifier, type, underlying_type,
17921 attributes, scoped_enum_p, &is_new_type);
17922
17923 /* If the next token is not '{' it is an opaque-enum-specifier or an
17924 elaborated-type-specifier. */
17925 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17926 {
17927 timevar_push (TV_PARSE_ENUM);
17928 if (nested_name_specifier
17929 && nested_name_specifier != error_mark_node)
17930 {
17931 /* The following catches invalid code such as:
17932 enum class S<int>::E { A, B, C }; */
17933 if (!processing_specialization
17934 && CLASS_TYPE_P (nested_name_specifier)
17935 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17936 error_at (type_start_token->location, "cannot add an enumerator "
17937 "list to a template instantiation");
17938
17939 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17940 {
17941 error_at (type_start_token->location,
17942 "%<%T::%E%> has not been declared",
17943 TYPE_CONTEXT (nested_name_specifier),
17944 nested_name_specifier);
17945 type = error_mark_node;
17946 }
17947 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17948 && !CLASS_TYPE_P (nested_name_specifier))
17949 {
17950 error_at (type_start_token->location, "nested name specifier "
17951 "%qT for enum declaration does not name a class "
17952 "or namespace", nested_name_specifier);
17953 type = error_mark_node;
17954 }
17955 /* If that scope does not contain the scope in which the
17956 class was originally declared, the program is invalid. */
17957 else if (prev_scope && !is_ancestor (prev_scope,
17958 nested_name_specifier))
17959 {
17960 if (at_namespace_scope_p ())
17961 error_at (type_start_token->location,
17962 "declaration of %qD in namespace %qD which does not "
17963 "enclose %qD",
17964 type, prev_scope, nested_name_specifier);
17965 else
17966 error_at (type_start_token->location,
17967 "declaration of %qD in %qD which does not "
17968 "enclose %qD",
17969 type, prev_scope, nested_name_specifier);
17970 type = error_mark_node;
17971 }
17972 /* If that scope is the scope where the declaration is being placed
17973 the program is invalid. */
17974 else if (CLASS_TYPE_P (nested_name_specifier)
17975 && CLASS_TYPE_P (prev_scope)
17976 && same_type_p (nested_name_specifier, prev_scope))
17977 {
17978 permerror (type_start_token->location,
17979 "extra qualification not allowed");
17980 nested_name_specifier = NULL_TREE;
17981 }
17982 }
17983
17984 if (scoped_enum_p)
17985 begin_scope (sk_scoped_enum, type);
17986
17987 /* Consume the opening brace. */
17988 cp_lexer_consume_token (parser->lexer);
17989
17990 if (type == error_mark_node)
17991 ; /* Nothing to add */
17992 else if (OPAQUE_ENUM_P (type)
17993 || (cxx_dialect > cxx98 && processing_specialization))
17994 {
17995 new_value_list = true;
17996 SET_OPAQUE_ENUM_P (type, false);
17997 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17998 }
17999 else
18000 {
18001 error_at (type_start_token->location,
18002 "multiple definition of %q#T", type);
18003 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18004 "previous definition here");
18005 type = error_mark_node;
18006 }
18007
18008 if (type == error_mark_node)
18009 cp_parser_skip_to_end_of_block_or_statement (parser);
18010 /* If the next token is not '}', then there are some enumerators. */
18011 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18012 {
18013 if (is_unnamed && !scoped_enum_p)
18014 pedwarn (type_start_token->location, OPT_Wpedantic,
18015 "ISO C++ forbids empty unnamed enum");
18016 }
18017 else
18018 cp_parser_enumerator_list (parser, type);
18019
18020 /* Consume the final '}'. */
18021 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18022
18023 if (scoped_enum_p)
18024 finish_scope ();
18025 timevar_pop (TV_PARSE_ENUM);
18026 }
18027 else
18028 {
18029 /* If a ';' follows, then it is an opaque-enum-specifier
18030 and additional restrictions apply. */
18031 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18032 {
18033 if (is_unnamed)
18034 error_at (type_start_token->location,
18035 "opaque-enum-specifier without name");
18036 else if (nested_name_specifier)
18037 error_at (type_start_token->location,
18038 "opaque-enum-specifier must use a simple identifier");
18039 }
18040 }
18041
18042 /* Look for trailing attributes to apply to this enumeration, and
18043 apply them if appropriate. */
18044 if (cp_parser_allow_gnu_extensions_p (parser))
18045 {
18046 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18047 cplus_decl_attributes (&type,
18048 trailing_attr,
18049 (int) ATTR_FLAG_TYPE_IN_PLACE);
18050 }
18051
18052 /* Finish up the enumeration. */
18053 if (type != error_mark_node)
18054 {
18055 if (new_value_list)
18056 finish_enum_value_list (type);
18057 if (is_new_type)
18058 finish_enum (type);
18059 }
18060
18061 if (nested_name_specifier)
18062 {
18063 if (CLASS_TYPE_P (nested_name_specifier))
18064 {
18065 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18066 pop_scope (nested_name_specifier);
18067 }
18068 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18069 {
18070 pop_nested_namespace (nested_name_specifier);
18071 }
18072 }
18073 out:
18074 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18075 return type;
18076}
18077
18078/* Parse an enumerator-list. The enumerators all have the indicated
18079 TYPE.
18080
18081 enumerator-list:
18082 enumerator-definition
18083 enumerator-list , enumerator-definition */
18084
18085static void
18086cp_parser_enumerator_list (cp_parser* parser, tree type)
18087{
18088 while (true)
18089 {
18090 /* Parse an enumerator-definition. */
18091 cp_parser_enumerator_definition (parser, type);
18092
18093 /* If the next token is not a ',', we've reached the end of
18094 the list. */
18095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18096 break;
18097 /* Otherwise, consume the `,' and keep going. */
18098 cp_lexer_consume_token (parser->lexer);
18099 /* If the next token is a `}', there is a trailing comma. */
18100 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18101 {
18102 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18103 pedwarn (input_location, OPT_Wpedantic,
18104 "comma at end of enumerator list");
18105 break;
18106 }
18107 }
18108}
18109
18110/* Parse an enumerator-definition. The enumerator has the indicated
18111 TYPE.
18112
18113 enumerator-definition:
18114 enumerator
18115 enumerator = constant-expression
18116
18117 enumerator:
18118 identifier
18119
18120 GNU Extensions:
18121
18122 enumerator-definition:
18123 enumerator attributes [opt]
18124 enumerator attributes [opt] = constant-expression */
18125
18126static void
18127cp_parser_enumerator_definition (cp_parser* parser, tree type)
18128{
18129 tree identifier;
18130 tree value;
18131 location_t loc;
18132
18133 /* Save the input location because we are interested in the location
18134 of the identifier and not the location of the explicit value. */
18135 loc = cp_lexer_peek_token (parser->lexer)->location;
18136
18137 /* Look for the identifier. */
18138 identifier = cp_parser_identifier (parser);
18139 if (identifier == error_mark_node)
18140 return;
18141
18142 /* Parse any specified attributes. */
18143 tree attrs = cp_parser_attributes_opt (parser);
18144
18145 /* If the next token is an '=', then there is an explicit value. */
18146 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18147 {
18148 /* Consume the `=' token. */
18149 cp_lexer_consume_token (parser->lexer);
18150 /* Parse the value. */
18151 value = cp_parser_constant_expression (parser);
18152 }
18153 else
18154 value = NULL_TREE;
18155
18156 /* If we are processing a template, make sure the initializer of the
18157 enumerator doesn't contain any bare template parameter pack. */
18158 if (check_for_bare_parameter_packs (value))
18159 value = error_mark_node;
18160
18161 /* Create the enumerator. */
18162 build_enumerator (identifier, value, type, attrs, loc);
18163}
18164
18165/* Parse a namespace-name.
18166
18167 namespace-name:
18168 original-namespace-name
18169 namespace-alias
18170
18171 Returns the NAMESPACE_DECL for the namespace. */
18172
18173static tree
18174cp_parser_namespace_name (cp_parser* parser)
18175{
18176 tree identifier;
18177 tree namespace_decl;
18178
18179 cp_token *token = cp_lexer_peek_token (parser->lexer);
18180
18181 /* Get the name of the namespace. */
18182 identifier = cp_parser_identifier (parser);
18183 if (identifier == error_mark_node)
18184 return error_mark_node;
18185
18186 /* Look up the identifier in the currently active scope. Look only
18187 for namespaces, due to:
18188
18189 [basic.lookup.udir]
18190
18191 When looking up a namespace-name in a using-directive or alias
18192 definition, only namespace names are considered.
18193
18194 And:
18195
18196 [basic.lookup.qual]
18197
18198 During the lookup of a name preceding the :: scope resolution
18199 operator, object, function, and enumerator names are ignored.
18200
18201 (Note that cp_parser_qualifying_entity only calls this
18202 function if the token after the name is the scope resolution
18203 operator.) */
18204 namespace_decl = cp_parser_lookup_name (parser, identifier,
18205 none_type,
18206 /*is_template=*/false,
18207 /*is_namespace=*/true,
18208 /*check_dependency=*/true,
18209 /*ambiguous_decls=*/NULL,
18210 token->location);
18211 /* If it's not a namespace, issue an error. */
18212 if (namespace_decl == error_mark_node
18213 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18214 {
18215 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18216 error_at (token->location, "%qD is not a namespace-name", identifier);
18217 cp_parser_error (parser, "expected namespace-name");
18218 namespace_decl = error_mark_node;
18219 }
18220
18221 return namespace_decl;
18222}
18223
18224/* Parse a namespace-definition.
18225
18226 namespace-definition:
18227 named-namespace-definition
18228 unnamed-namespace-definition
18229
18230 named-namespace-definition:
18231 original-namespace-definition
18232 extension-namespace-definition
18233
18234 original-namespace-definition:
18235 namespace identifier { namespace-body }
18236
18237 extension-namespace-definition:
18238 namespace original-namespace-name { namespace-body }
18239
18240 unnamed-namespace-definition:
18241 namespace { namespace-body } */
18242
18243static void
18244cp_parser_namespace_definition (cp_parser* parser)
18245{
18246 tree identifier, attribs;
18247 bool has_visibility;
18248 bool is_inline;
18249 cp_token* token;
18250 int nested_definition_count = 0;
18251
18252 cp_ensure_no_omp_declare_simd (parser);
18253 cp_ensure_no_oacc_routine (parser);
18254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18255 {
18256 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18257 is_inline = true;
18258 cp_lexer_consume_token (parser->lexer);
18259 }
18260 else
18261 is_inline = false;
18262
18263 /* Look for the `namespace' keyword. */
18264 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18265
18266 /* Parse any specified attributes before the identifier. */
18267 attribs = cp_parser_attributes_opt (parser);
18268
18269 /* Get the name of the namespace. We do not attempt to distinguish
18270 between an original-namespace-definition and an
18271 extension-namespace-definition at this point. The semantic
18272 analysis routines are responsible for that. */
18273 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18274 identifier = cp_parser_identifier (parser);
18275 else
18276 identifier = NULL_TREE;
18277
18278 /* Parse any specified attributes after the identifier. */
18279 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18280 if (post_ident_attribs)
18281 {
18282 if (attribs)
18283 attribs = attr_chainon (attribs, post_ident_attribs);
18284 else
18285 attribs = post_ident_attribs;
18286 }
18287
18288 /* Start the namespace. */
18289 bool ok = push_namespace (identifier);
18290
18291 /* Parse any nested namespace definition. */
18292 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18293 {
18294 if (attribs)
18295 error_at (token->location, "a nested namespace definition cannot have attributes");
18296 if (cxx_dialect < cxx1z)
18297 pedwarn (input_location, OPT_Wpedantic,
18298 "nested namespace definitions only available with "
18299 "-std=c++1z or -std=gnu++1z");
18300 if (is_inline)
18301 error_at (token->location, "a nested namespace definition cannot be inline");
18302 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18303 {
18304 cp_lexer_consume_token (parser->lexer);
18305 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18306 identifier = cp_parser_identifier (parser);
18307 else
18308 {
18309 cp_parser_error (parser, "nested identifier required");
18310 break;
18311 }
18312 if (push_namespace (identifier))
18313 ++nested_definition_count;
18314 }
18315 }
18316
18317 /* Look for the `{' to validate starting the namespace. */
18318 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18319
18320 /* "inline namespace" is equivalent to a stub namespace definition
18321 followed by a strong using directive. */
18322 if (is_inline && ok)
18323 {
18324 tree name_space = current_namespace;
18325 /* Set up namespace association. */
18326 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18327 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18328 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18329 /* Import the contents of the inline namespace. */
18330 pop_namespace ();
18331 do_using_directive (name_space);
18332 push_namespace (identifier);
18333 }
18334
18335 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18336
18337 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18338
18339 /* Parse the body of the namespace. */
18340 cp_parser_namespace_body (parser);
18341
18342 if (has_visibility)
18343 pop_visibility (1);
18344
18345 /* Finish the nested namespace definitions. */
18346 while (nested_definition_count--)
18347 pop_namespace ();
18348
18349 /* Finish the namespace. */
18350 if (ok)
18351 pop_namespace ();
18352 /* Look for the final `}'. */
18353 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18354}
18355
18356/* Parse a namespace-body.
18357
18358 namespace-body:
18359 declaration-seq [opt] */
18360
18361static void
18362cp_parser_namespace_body (cp_parser* parser)
18363{
18364 cp_parser_declaration_seq_opt (parser);
18365}
18366
18367/* Parse a namespace-alias-definition.
18368
18369 namespace-alias-definition:
18370 namespace identifier = qualified-namespace-specifier ; */
18371
18372static void
18373cp_parser_namespace_alias_definition (cp_parser* parser)
18374{
18375 tree identifier;
18376 tree namespace_specifier;
18377
18378 cp_token *token = cp_lexer_peek_token (parser->lexer);
18379
18380 /* Look for the `namespace' keyword. */
18381 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18382 /* Look for the identifier. */
18383 identifier = cp_parser_identifier (parser);
18384 if (identifier == error_mark_node)
18385 return;
18386 /* Look for the `=' token. */
18387 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18388 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18389 {
18390 error_at (token->location, "%<namespace%> definition is not allowed here");
18391 /* Skip the definition. */
18392 cp_lexer_consume_token (parser->lexer);
18393 if (cp_parser_skip_to_closing_brace (parser))
18394 cp_lexer_consume_token (parser->lexer);
18395 return;
18396 }
18397 cp_parser_require (parser, CPP_EQ, RT_EQ);
18398 /* Look for the qualified-namespace-specifier. */
18399 namespace_specifier
18400 = cp_parser_qualified_namespace_specifier (parser);
18401 /* Look for the `;' token. */
18402 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18403
18404 /* Register the alias in the symbol table. */
18405 do_namespace_alias (identifier, namespace_specifier);
18406}
18407
18408/* Parse a qualified-namespace-specifier.
18409
18410 qualified-namespace-specifier:
18411 :: [opt] nested-name-specifier [opt] namespace-name
18412
18413 Returns a NAMESPACE_DECL corresponding to the specified
18414 namespace. */
18415
18416static tree
18417cp_parser_qualified_namespace_specifier (cp_parser* parser)
18418{
18419 /* Look for the optional `::'. */
18420 cp_parser_global_scope_opt (parser,
18421 /*current_scope_valid_p=*/false);
18422
18423 /* Look for the optional nested-name-specifier. */
18424 cp_parser_nested_name_specifier_opt (parser,
18425 /*typename_keyword_p=*/false,
18426 /*check_dependency_p=*/true,
18427 /*type_p=*/false,
18428 /*is_declaration=*/true);
18429
18430 return cp_parser_namespace_name (parser);
18431}
18432
18433/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18434 access declaration.
18435
18436 using-declaration:
18437 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18438 using :: unqualified-id ;
18439
18440 access-declaration:
18441 qualified-id ;
18442
18443 */
18444
18445static bool
18446cp_parser_using_declaration (cp_parser* parser,
18447 bool access_declaration_p)
18448{
18449 cp_token *token;
18450 bool typename_p = false;
18451 bool global_scope_p;
18452 tree decl;
18453 tree identifier;
18454 tree qscope;
18455 int oldcount = errorcount;
18456 cp_token *diag_token = NULL;
18457
18458 if (access_declaration_p)
18459 {
18460 diag_token = cp_lexer_peek_token (parser->lexer);
18461 cp_parser_parse_tentatively (parser);
18462 }
18463 else
18464 {
18465 /* Look for the `using' keyword. */
18466 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18467
18468 again:
18469 /* Peek at the next token. */
18470 token = cp_lexer_peek_token (parser->lexer);
18471 /* See if it's `typename'. */
18472 if (token->keyword == RID_TYPENAME)
18473 {
18474 /* Remember that we've seen it. */
18475 typename_p = true;
18476 /* Consume the `typename' token. */
18477 cp_lexer_consume_token (parser->lexer);
18478 }
18479 }
18480
18481 /* Look for the optional global scope qualification. */
18482 global_scope_p
18483 = (cp_parser_global_scope_opt (parser,
18484 /*current_scope_valid_p=*/false)
18485 != NULL_TREE);
18486
18487 /* If we saw `typename', or didn't see `::', then there must be a
18488 nested-name-specifier present. */
18489 if (typename_p || !global_scope_p)
18490 {
18491 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18492 /*check_dependency_p=*/true,
18493 /*type_p=*/false,
18494 /*is_declaration=*/true);
18495 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18496 {
18497 cp_parser_skip_to_end_of_block_or_statement (parser);
18498 return false;
18499 }
18500 }
18501 /* Otherwise, we could be in either of the two productions. In that
18502 case, treat the nested-name-specifier as optional. */
18503 else
18504 qscope = cp_parser_nested_name_specifier_opt (parser,
18505 /*typename_keyword_p=*/false,
18506 /*check_dependency_p=*/true,
18507 /*type_p=*/false,
18508 /*is_declaration=*/true);
18509 if (!qscope)
18510 qscope = global_namespace;
18511 else if (UNSCOPED_ENUM_P (qscope))
18512 qscope = CP_TYPE_CONTEXT (qscope);
18513
18514 if (access_declaration_p && cp_parser_error_occurred (parser))
18515 /* Something has already gone wrong; there's no need to parse
18516 further. Since an error has occurred, the return value of
18517 cp_parser_parse_definitely will be false, as required. */
18518 return cp_parser_parse_definitely (parser);
18519
18520 token = cp_lexer_peek_token (parser->lexer);
18521 /* Parse the unqualified-id. */
18522 identifier = cp_parser_unqualified_id (parser,
18523 /*template_keyword_p=*/false,
18524 /*check_dependency_p=*/true,
18525 /*declarator_p=*/true,
18526 /*optional_p=*/false);
18527
18528 if (access_declaration_p)
18529 {
18530 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18531 cp_parser_simulate_error (parser);
18532 if (!cp_parser_parse_definitely (parser))
18533 return false;
18534 }
18535 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18536 {
18537 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18538 if (cxx_dialect < cxx1z
18539 && !in_system_header_at (ell->location))
18540 pedwarn (ell->location, 0,
18541 "pack expansion in using-declaration only available "
18542 "with -std=c++1z or -std=gnu++1z");
18543 qscope = make_pack_expansion (qscope);
18544 }
18545
18546 /* The function we call to handle a using-declaration is different
18547 depending on what scope we are in. */
18548 if (qscope == error_mark_node || identifier == error_mark_node)
18549 ;
18550 else if (!identifier_p (identifier)
18551 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18552 /* [namespace.udecl]
18553
18554 A using declaration shall not name a template-id. */
18555 error_at (token->location,
18556 "a template-id may not appear in a using-declaration");
18557 else
18558 {
18559 if (at_class_scope_p ())
18560 {
18561 /* Create the USING_DECL. */
18562 decl = do_class_using_decl (qscope, identifier);
18563
18564 if (decl && typename_p)
18565 USING_DECL_TYPENAME_P (decl) = 1;
18566
18567 if (check_for_bare_parameter_packs (decl))
18568 {
18569 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18570 return false;
18571 }
18572 else
18573 /* Add it to the list of members in this class. */
18574 finish_member_declaration (decl);
18575 }
18576 else
18577 {
18578 decl = cp_parser_lookup_name_simple (parser,
18579 identifier,
18580 token->location);
18581 if (decl == error_mark_node)
18582 cp_parser_name_lookup_error (parser, identifier,
18583 decl, NLE_NULL,
18584 token->location);
18585 else if (check_for_bare_parameter_packs (decl))
18586 {
18587 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18588 return false;
18589 }
18590 else if (!at_namespace_scope_p ())
18591 do_local_using_decl (decl, qscope, identifier);
18592 else
18593 do_toplevel_using_decl (decl, qscope, identifier);
18594 }
18595 }
18596
18597 if (!access_declaration_p
18598 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18599 {
18600 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18601 if (cxx_dialect < cxx1z)
18602 pedwarn (comma->location, 0,
18603 "comma-separated list in using-declaration only available "
18604 "with -std=c++1z or -std=gnu++1z");
18605 goto again;
18606 }
18607
18608 /* Look for the final `;'. */
18609 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18610
18611 if (access_declaration_p && errorcount == oldcount)
18612 warning_at (diag_token->location, OPT_Wdeprecated,
18613 "access declarations are deprecated "
18614 "in favour of using-declarations; "
18615 "suggestion: add the %<using%> keyword");
18616
18617 return true;
18618}
18619
18620/* Parse an alias-declaration.
18621
18622 alias-declaration:
18623 using identifier attribute-specifier-seq [opt] = type-id */
18624
18625static tree
18626cp_parser_alias_declaration (cp_parser* parser)
18627{
18628 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18629 location_t id_location;
18630 cp_declarator *declarator;
18631 cp_decl_specifier_seq decl_specs;
18632 bool member_p;
18633 const char *saved_message = NULL;
18634
18635 /* Look for the `using' keyword. */
18636 cp_token *using_token
18637 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18638 if (using_token == NULL)
18639 return error_mark_node;
18640
18641 id_location = cp_lexer_peek_token (parser->lexer)->location;
18642 id = cp_parser_identifier (parser);
18643 if (id == error_mark_node)
18644 return error_mark_node;
18645
18646 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18647 attributes = cp_parser_attributes_opt (parser);
18648 if (attributes == error_mark_node)
18649 return error_mark_node;
18650
18651 cp_parser_require (parser, CPP_EQ, RT_EQ);
18652
18653 if (cp_parser_error_occurred (parser))
18654 return error_mark_node;
18655
18656 cp_parser_commit_to_tentative_parse (parser);
18657
18658 /* Now we are going to parse the type-id of the declaration. */
18659
18660 /*
18661 [dcl.type]/3 says:
18662
18663 "A type-specifier-seq shall not define a class or enumeration
18664 unless it appears in the type-id of an alias-declaration (7.1.3) that
18665 is not the declaration of a template-declaration."
18666
18667 In other words, if we currently are in an alias template, the
18668 type-id should not define a type.
18669
18670 So let's set parser->type_definition_forbidden_message in that
18671 case; cp_parser_check_type_definition (called by
18672 cp_parser_class_specifier) will then emit an error if a type is
18673 defined in the type-id. */
18674 if (parser->num_template_parameter_lists)
18675 {
18676 saved_message = parser->type_definition_forbidden_message;
18677 parser->type_definition_forbidden_message =
18678 G_("types may not be defined in alias template declarations");
18679 }
18680
18681 type = cp_parser_type_id (parser);
18682
18683 /* Restore the error message if need be. */
18684 if (parser->num_template_parameter_lists)
18685 parser->type_definition_forbidden_message = saved_message;
18686
18687 if (type == error_mark_node
18688 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18689 {
18690 cp_parser_skip_to_end_of_block_or_statement (parser);
18691 return error_mark_node;
18692 }
18693
18694 /* A typedef-name can also be introduced by an alias-declaration. The
18695 identifier following the using keyword becomes a typedef-name. It has
18696 the same semantics as if it were introduced by the typedef
18697 specifier. In particular, it does not define a new type and it shall
18698 not appear in the type-id. */
18699
18700 clear_decl_specs (&decl_specs);
18701 decl_specs.type = type;
18702 if (attributes != NULL_TREE)
18703 {
18704 decl_specs.attributes = attributes;
18705 set_and_check_decl_spec_loc (&decl_specs,
18706 ds_attribute,
18707 attrs_token);
18708 }
18709 set_and_check_decl_spec_loc (&decl_specs,
18710 ds_typedef,
18711 using_token);
18712 set_and_check_decl_spec_loc (&decl_specs,
18713 ds_alias,
18714 using_token);
18715
18716 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18717 declarator->id_loc = id_location;
18718
18719 member_p = at_class_scope_p ();
18720 if (member_p)
18721 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18722 NULL_TREE, attributes);
18723 else
18724 decl = start_decl (declarator, &decl_specs, 0,
18725 attributes, NULL_TREE, &pushed_scope);
18726 if (decl == error_mark_node)
18727 return decl;
18728
18729 // Attach constraints to the alias declaration.
18730 if (flag_concepts && current_template_parms)
18731 {
18732 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18733 tree constr = build_constraints (reqs, NULL_TREE);
18734 set_constraints (decl, constr);
18735 }
18736
18737 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18738
18739 if (pushed_scope)
18740 pop_scope (pushed_scope);
18741
18742 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18743 added into the symbol table; otherwise, return the TYPE_DECL. */
18744 if (DECL_LANG_SPECIFIC (decl)
18745 && DECL_TEMPLATE_INFO (decl)
18746 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18747 {
18748 decl = DECL_TI_TEMPLATE (decl);
18749 if (member_p)
18750 check_member_template (decl);
18751 }
18752
18753 return decl;
18754}
18755
18756/* Parse a using-directive.
18757
18758 using-directive:
18759 using namespace :: [opt] nested-name-specifier [opt]
18760 namespace-name ; */
18761
18762static void
18763cp_parser_using_directive (cp_parser* parser)
18764{
18765 tree namespace_decl;
18766 tree attribs;
18767
18768 /* Look for the `using' keyword. */
18769 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18770 /* And the `namespace' keyword. */
18771 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18772 /* Look for the optional `::' operator. */
18773 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18774 /* And the optional nested-name-specifier. */
18775 cp_parser_nested_name_specifier_opt (parser,
18776 /*typename_keyword_p=*/false,
18777 /*check_dependency_p=*/true,
18778 /*type_p=*/false,
18779 /*is_declaration=*/true);
18780 /* Get the namespace being used. */
18781 namespace_decl = cp_parser_namespace_name (parser);
18782 /* And any specified attributes. */
18783 attribs = cp_parser_attributes_opt (parser);
18784 /* Update the symbol table. */
18785 parse_using_directive (namespace_decl, attribs);
18786 /* Look for the final `;'. */
18787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18788}
18789
18790/* Parse an asm-definition.
18791
18792 asm-definition:
18793 asm ( string-literal ) ;
18794
18795 GNU Extension:
18796
18797 asm-definition:
18798 asm volatile [opt] ( string-literal ) ;
18799 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18800 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18801 : asm-operand-list [opt] ) ;
18802 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18803 : asm-operand-list [opt]
18804 : asm-clobber-list [opt] ) ;
18805 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18806 : asm-clobber-list [opt]
18807 : asm-goto-list ) ; */
18808
18809static void
18810cp_parser_asm_definition (cp_parser* parser)
18811{
18812 tree string;
18813 tree outputs = NULL_TREE;
18814 tree inputs = NULL_TREE;
18815 tree clobbers = NULL_TREE;
18816 tree labels = NULL_TREE;
18817 tree asm_stmt;
18818 bool volatile_p = false;
18819 bool extended_p = false;
18820 bool invalid_inputs_p = false;
18821 bool invalid_outputs_p = false;
18822 bool goto_p = false;
18823 required_token missing = RT_NONE;
18824
18825 /* Look for the `asm' keyword. */
18826 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18827
18828 if (parser->in_function_body
18829 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18830 {
18831 error ("%<asm%> in %<constexpr%> function");
18832 cp_function_chain->invalid_constexpr = true;
18833 }
18834
18835 /* See if the next token is `volatile'. */
18836 if (cp_parser_allow_gnu_extensions_p (parser)
18837 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18838 {
18839 /* Remember that we saw the `volatile' keyword. */
18840 volatile_p = true;
18841 /* Consume the token. */
18842 cp_lexer_consume_token (parser->lexer);
18843 }
18844 if (cp_parser_allow_gnu_extensions_p (parser)
18845 && parser->in_function_body
18846 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18847 {
18848 /* Remember that we saw the `goto' keyword. */
18849 goto_p = true;
18850 /* Consume the token. */
18851 cp_lexer_consume_token (parser->lexer);
18852 }
18853 /* Look for the opening `('. */
18854 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18855 return;
18856 /* Look for the string. */
18857 string = cp_parser_string_literal (parser, false, false);
18858 if (string == error_mark_node)
18859 {
18860 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18861 /*consume_paren=*/true);
18862 return;
18863 }
18864
18865 /* If we're allowing GNU extensions, check for the extended assembly
18866 syntax. Unfortunately, the `:' tokens need not be separated by
18867 a space in C, and so, for compatibility, we tolerate that here
18868 too. Doing that means that we have to treat the `::' operator as
18869 two `:' tokens. */
18870 if (cp_parser_allow_gnu_extensions_p (parser)
18871 && parser->in_function_body
18872 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18873 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18874 {
18875 bool inputs_p = false;
18876 bool clobbers_p = false;
18877 bool labels_p = false;
18878
18879 /* The extended syntax was used. */
18880 extended_p = true;
18881
18882 /* Look for outputs. */
18883 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18884 {
18885 /* Consume the `:'. */
18886 cp_lexer_consume_token (parser->lexer);
18887 /* Parse the output-operands. */
18888 if (cp_lexer_next_token_is_not (parser->lexer,
18889 CPP_COLON)
18890 && cp_lexer_next_token_is_not (parser->lexer,
18891 CPP_SCOPE)
18892 && cp_lexer_next_token_is_not (parser->lexer,
18893 CPP_CLOSE_PAREN)
18894 && !goto_p)
18895 {
18896 outputs = cp_parser_asm_operand_list (parser);
18897 if (outputs == error_mark_node)
18898 invalid_outputs_p = true;
18899 }
18900 }
18901 /* If the next token is `::', there are no outputs, and the
18902 next token is the beginning of the inputs. */
18903 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18904 /* The inputs are coming next. */
18905 inputs_p = true;
18906
18907 /* Look for inputs. */
18908 if (inputs_p
18909 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18910 {
18911 /* Consume the `:' or `::'. */
18912 cp_lexer_consume_token (parser->lexer);
18913 /* Parse the output-operands. */
18914 if (cp_lexer_next_token_is_not (parser->lexer,
18915 CPP_COLON)
18916 && cp_lexer_next_token_is_not (parser->lexer,
18917 CPP_SCOPE)
18918 && cp_lexer_next_token_is_not (parser->lexer,
18919 CPP_CLOSE_PAREN))
18920 {
18921 inputs = cp_parser_asm_operand_list (parser);
18922 if (inputs == error_mark_node)
18923 invalid_inputs_p = true;
18924 }
18925 }
18926 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18927 /* The clobbers are coming next. */
18928 clobbers_p = true;
18929
18930 /* Look for clobbers. */
18931 if (clobbers_p
18932 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18933 {
18934 clobbers_p = true;
18935 /* Consume the `:' or `::'. */
18936 cp_lexer_consume_token (parser->lexer);
18937 /* Parse the clobbers. */
18938 if (cp_lexer_next_token_is_not (parser->lexer,
18939 CPP_COLON)
18940 && cp_lexer_next_token_is_not (parser->lexer,
18941 CPP_CLOSE_PAREN))
18942 clobbers = cp_parser_asm_clobber_list (parser);
18943 }
18944 else if (goto_p
18945 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18946 /* The labels are coming next. */
18947 labels_p = true;
18948
18949 /* Look for labels. */
18950 if (labels_p
18951 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18952 {
18953 labels_p = true;
18954 /* Consume the `:' or `::'. */
18955 cp_lexer_consume_token (parser->lexer);
18956 /* Parse the labels. */
18957 labels = cp_parser_asm_label_list (parser);
18958 }
18959
18960 if (goto_p && !labels_p)
18961 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18962 }
18963 else if (goto_p)
18964 missing = RT_COLON_SCOPE;
18965
18966 /* Look for the closing `)'. */
18967 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18968 missing ? missing : RT_CLOSE_PAREN))
18969 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18970 /*consume_paren=*/true);
18971 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18972
18973 if (!invalid_inputs_p && !invalid_outputs_p)
18974 {
18975 /* Create the ASM_EXPR. */
18976 if (parser->in_function_body)
18977 {
18978 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18979 inputs, clobbers, labels);
18980 /* If the extended syntax was not used, mark the ASM_EXPR. */
18981 if (!extended_p)
18982 {
18983 tree temp = asm_stmt;
18984 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18985 temp = TREE_OPERAND (temp, 0);
18986
18987 ASM_INPUT_P (temp) = 1;
18988 }
18989 }
18990 else
18991 symtab->finalize_toplevel_asm (string);
18992 }
18993}
18994
18995/* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18996 type that comes from the decl-specifier-seq. */
18997
18998static tree
18999strip_declarator_types (tree type, cp_declarator *declarator)
19000{
19001 for (cp_declarator *d = declarator; d;)
19002 switch (d->kind)
19003 {
19004 case cdk_id:
19005 case cdk_decomp:
19006 case cdk_error:
19007 d = NULL;
19008 break;
19009
19010 default:
19011 if (TYPE_PTRMEMFUNC_P (type))
19012 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19013 type = TREE_TYPE (type);
19014 d = d->declarator;
19015 break;
19016 }
19017
19018 return type;
19019}
19020
19021/* Declarators [gram.dcl.decl] */
19022
19023/* Parse an init-declarator.
19024
19025 init-declarator:
19026 declarator initializer [opt]
19027
19028 GNU Extension:
19029
19030 init-declarator:
19031 declarator asm-specification [opt] attributes [opt] initializer [opt]
19032
19033 function-definition:
19034 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19035 function-body
19036 decl-specifier-seq [opt] declarator function-try-block
19037
19038 GNU Extension:
19039
19040 function-definition:
19041 __extension__ function-definition
19042
19043 TM Extension:
19044
19045 function-definition:
19046 decl-specifier-seq [opt] declarator function-transaction-block
19047
19048 The DECL_SPECIFIERS apply to this declarator. Returns a
19049 representation of the entity declared. If MEMBER_P is TRUE, then
19050 this declarator appears in a class scope. The new DECL created by
19051 this declarator is returned.
19052
19053 The CHECKS are access checks that should be performed once we know
19054 what entity is being declared (and, therefore, what classes have
19055 befriended it).
19056
19057 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19058 for a function-definition here as well. If the declarator is a
19059 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19060 be TRUE upon return. By that point, the function-definition will
19061 have been completely parsed.
19062
19063 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19064 is FALSE.
19065
19066 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19067 parsed declaration if it is an uninitialized single declarator not followed
19068 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19069 if present, will not be consumed. If returned, this declarator will be
19070 created with SD_INITIALIZED but will not call cp_finish_decl.
19071
19072 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19073 and there is an initializer, the pointed location_t is set to the
19074 location of the '=' or `(', or '{' in C++11 token introducing the
19075 initializer. */
19076
19077static tree
19078cp_parser_init_declarator (cp_parser* parser,
19079 cp_decl_specifier_seq *decl_specifiers,
19080 vec<deferred_access_check, va_gc> *checks,
19081 bool function_definition_allowed_p,
19082 bool member_p,
19083 int declares_class_or_enum,
19084 bool* function_definition_p,
19085 tree* maybe_range_for_decl,
19086 location_t* init_loc,
19087 tree* auto_result)
19088{
19089 cp_token *token = NULL, *asm_spec_start_token = NULL,
19090 *attributes_start_token = NULL;
19091 cp_declarator *declarator;
19092 tree prefix_attributes;
19093 tree attributes = NULL;
19094 tree asm_specification;
19095 tree initializer;
19096 tree decl = NULL_TREE;
19097 tree scope;
19098 int is_initialized;
19099 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19100 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19101 "(...)". */
19102 enum cpp_ttype initialization_kind;
19103 bool is_direct_init = false;
19104 bool is_non_constant_init;
19105 int ctor_dtor_or_conv_p;
19106 bool friend_p = cp_parser_friend_p (decl_specifiers);
19107 tree pushed_scope = NULL_TREE;
19108 bool range_for_decl_p = false;
19109 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19110 location_t tmp_init_loc = UNKNOWN_LOCATION;
19111
19112 /* Gather the attributes that were provided with the
19113 decl-specifiers. */
19114 prefix_attributes = decl_specifiers->attributes;
19115
19116 /* Assume that this is not the declarator for a function
19117 definition. */
19118 if (function_definition_p)
19119 *function_definition_p = false;
19120
19121 /* Default arguments are only permitted for function parameters. */
19122 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19123 parser->default_arg_ok_p = false;
19124
19125 /* Defer access checks while parsing the declarator; we cannot know
19126 what names are accessible until we know what is being
19127 declared. */
19128 resume_deferring_access_checks ();
19129
19130 token = cp_lexer_peek_token (parser->lexer);
19131
19132 /* Parse the declarator. */
19133 declarator
19134 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19135 &ctor_dtor_or_conv_p,
19136 /*parenthesized_p=*/NULL,
19137 member_p, friend_p);
19138 /* Gather up the deferred checks. */
19139 stop_deferring_access_checks ();
19140
19141 parser->default_arg_ok_p = saved_default_arg_ok_p;
19142
19143 /* If the DECLARATOR was erroneous, there's no need to go
19144 further. */
19145 if (declarator == cp_error_declarator)
19146 return error_mark_node;
19147
19148 /* Check that the number of template-parameter-lists is OK. */
19149 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19150 token->location))
19151 return error_mark_node;
19152
19153 if (declares_class_or_enum & 2)
19154 cp_parser_check_for_definition_in_return_type (declarator,
19155 decl_specifiers->type,
19156 decl_specifiers->locations[ds_type_spec]);
19157
19158 /* Figure out what scope the entity declared by the DECLARATOR is
19159 located in. `grokdeclarator' sometimes changes the scope, so
19160 we compute it now. */
19161 scope = get_scope_of_declarator (declarator);
19162
19163 /* Perform any lookups in the declared type which were thought to be
19164 dependent, but are not in the scope of the declarator. */
19165 decl_specifiers->type
19166 = maybe_update_decl_type (decl_specifiers->type, scope);
19167
19168 /* If we're allowing GNU extensions, look for an
19169 asm-specification. */
19170 if (cp_parser_allow_gnu_extensions_p (parser))
19171 {
19172 /* Look for an asm-specification. */
19173 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19174 asm_specification = cp_parser_asm_specification_opt (parser);
19175 }
19176 else
19177 asm_specification = NULL_TREE;
19178
19179 /* Look for attributes. */
19180 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19181 attributes = cp_parser_attributes_opt (parser);
19182
19183 /* Peek at the next token. */
19184 token = cp_lexer_peek_token (parser->lexer);
19185
19186 bool bogus_implicit_tmpl = false;
19187
19188 if (function_declarator_p (declarator))
19189 {
19190 /* Handle C++17 deduction guides. */
19191 if (!decl_specifiers->type
19192 && ctor_dtor_or_conv_p <= 0
19193 && cxx_dialect >= cxx1z)
19194 {
19195 cp_declarator *id = get_id_declarator (declarator);
19196 tree name = id->u.id.unqualified_name;
19197 parser->scope = id->u.id.qualifying_scope;
19198 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19199 if (tmpl
19200 && (DECL_CLASS_TEMPLATE_P (tmpl)
19201 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19202 {
19203 id->u.id.unqualified_name = dguide_name (tmpl);
19204 id->u.id.sfk = sfk_deduction_guide;
19205 ctor_dtor_or_conv_p = 1;
19206 }
19207 }
19208
19209 /* Check to see if the token indicates the start of a
19210 function-definition. */
19211 if (cp_parser_token_starts_function_definition_p (token))
19212 {
19213 if (!function_definition_allowed_p)
19214 {
19215 /* If a function-definition should not appear here, issue an
19216 error message. */
19217 cp_parser_error (parser,
19218 "a function-definition is not allowed here");
19219 return error_mark_node;
19220 }
19221
19222 location_t func_brace_location
19223 = cp_lexer_peek_token (parser->lexer)->location;
19224
19225 /* Neither attributes nor an asm-specification are allowed
19226 on a function-definition. */
19227 if (asm_specification)
19228 error_at (asm_spec_start_token->location,
19229 "an asm-specification is not allowed "
19230 "on a function-definition");
19231 if (attributes)
19232 error_at (attributes_start_token->location,
19233 "attributes are not allowed "
19234 "on a function-definition");
19235 /* This is a function-definition. */
19236 *function_definition_p = true;
19237
19238 /* Parse the function definition. */
19239 if (member_p)
19240 decl = cp_parser_save_member_function_body (parser,
19241 decl_specifiers,
19242 declarator,
19243 prefix_attributes);
19244 else
19245 decl =
19246 (cp_parser_function_definition_from_specifiers_and_declarator
19247 (parser, decl_specifiers, prefix_attributes, declarator));
19248
19249 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19250 {
19251 /* This is where the prologue starts... */
19252 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19253 = func_brace_location;
19254 }
19255
19256 return decl;
19257 }
19258 }
19259 else if (parser->fully_implicit_function_template_p)
19260 {
19261 /* A non-template declaration involving a function parameter list
19262 containing an implicit template parameter will be made into a
19263 template. If the resulting declaration is not going to be an
19264 actual function then finish the template scope here to prevent it.
19265 An error message will be issued once we have a decl to talk about.
19266
19267 FIXME probably we should do type deduction rather than create an
19268 implicit template, but the standard currently doesn't allow it. */
19269 bogus_implicit_tmpl = true;
19270 finish_fully_implicit_template (parser, NULL_TREE);
19271 }
19272
19273 /* [dcl.dcl]
19274
19275 Only in function declarations for constructors, destructors, type
19276 conversions, and deduction guides can the decl-specifier-seq be omitted.
19277
19278 We explicitly postpone this check past the point where we handle
19279 function-definitions because we tolerate function-definitions
19280 that are missing their return types in some modes. */
19281 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19282 {
19283 cp_parser_error (parser,
19284 "expected constructor, destructor, or type conversion");
19285 return error_mark_node;
19286 }
19287
19288 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19289 if (token->type == CPP_EQ
19290 || token->type == CPP_OPEN_PAREN
19291 || token->type == CPP_OPEN_BRACE)
19292 {
19293 is_initialized = SD_INITIALIZED;
19294 initialization_kind = token->type;
19295 if (maybe_range_for_decl)
19296 *maybe_range_for_decl = error_mark_node;
19297 tmp_init_loc = token->location;
19298 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19299 *init_loc = tmp_init_loc;
19300
19301 if (token->type == CPP_EQ
19302 && function_declarator_p (declarator))
19303 {
19304 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19305 if (t2->keyword == RID_DEFAULT)
19306 is_initialized = SD_DEFAULTED;
19307 else if (t2->keyword == RID_DELETE)
19308 is_initialized = SD_DELETED;
19309 }
19310 }
19311 else
19312 {
19313 /* If the init-declarator isn't initialized and isn't followed by a
19314 `,' or `;', it's not a valid init-declarator. */
19315 if (token->type != CPP_COMMA
19316 && token->type != CPP_SEMICOLON)
19317 {
19318 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19319 range_for_decl_p = true;
19320 else
19321 {
19322 if (!maybe_range_for_decl)
19323 cp_parser_error (parser, "expected initializer");
19324 return error_mark_node;
19325 }
19326 }
19327 is_initialized = SD_UNINITIALIZED;
19328 initialization_kind = CPP_EOF;
19329 }
19330
19331 /* Because start_decl has side-effects, we should only call it if we
19332 know we're going ahead. By this point, we know that we cannot
19333 possibly be looking at any other construct. */
19334 cp_parser_commit_to_tentative_parse (parser);
19335
19336 /* Enter the newly declared entry in the symbol table. If we're
19337 processing a declaration in a class-specifier, we wait until
19338 after processing the initializer. */
19339 if (!member_p)
19340 {
19341 if (parser->in_unbraced_linkage_specification_p)
19342 decl_specifiers->storage_class = sc_extern;
19343 decl = start_decl (declarator, decl_specifiers,
19344 range_for_decl_p? SD_INITIALIZED : is_initialized,
19345 attributes, prefix_attributes, &pushed_scope);
19346 cp_finalize_omp_declare_simd (parser, decl);
19347 cp_finalize_oacc_routine (parser, decl, false);
19348 /* Adjust location of decl if declarator->id_loc is more appropriate:
19349 set, and decl wasn't merged with another decl, in which case its
19350 location would be different from input_location, and more accurate. */
19351 if (DECL_P (decl)
19352 && declarator->id_loc != UNKNOWN_LOCATION
19353 && DECL_SOURCE_LOCATION (decl) == input_location)
19354 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19355 }
19356 else if (scope)
19357 /* Enter the SCOPE. That way unqualified names appearing in the
19358 initializer will be looked up in SCOPE. */
19359 pushed_scope = push_scope (scope);
19360
19361 /* Perform deferred access control checks, now that we know in which
19362 SCOPE the declared entity resides. */
19363 if (!member_p && decl)
19364 {
19365 tree saved_current_function_decl = NULL_TREE;
19366
19367 /* If the entity being declared is a function, pretend that we
19368 are in its scope. If it is a `friend', it may have access to
19369 things that would not otherwise be accessible. */
19370 if (TREE_CODE (decl) == FUNCTION_DECL)
19371 {
19372 saved_current_function_decl = current_function_decl;
19373 current_function_decl = decl;
19374 }
19375
19376 /* Perform access checks for template parameters. */
19377 cp_parser_perform_template_parameter_access_checks (checks);
19378
19379 /* Perform the access control checks for the declarator and the
19380 decl-specifiers. */
19381 perform_deferred_access_checks (tf_warning_or_error);
19382
19383 /* Restore the saved value. */
19384 if (TREE_CODE (decl) == FUNCTION_DECL)
19385 current_function_decl = saved_current_function_decl;
19386 }
19387
19388 /* Parse the initializer. */
19389 initializer = NULL_TREE;
19390 is_direct_init = false;
19391 is_non_constant_init = true;
19392 if (is_initialized)
19393 {
19394 if (function_declarator_p (declarator))
19395 {
19396 if (initialization_kind == CPP_EQ)
19397 initializer = cp_parser_pure_specifier (parser);
19398 else
19399 {
19400 /* If the declaration was erroneous, we don't really
19401 know what the user intended, so just silently
19402 consume the initializer. */
19403 if (decl != error_mark_node)
19404 error_at (tmp_init_loc, "initializer provided for function");
19405 cp_parser_skip_to_closing_parenthesis (parser,
19406 /*recovering=*/true,
19407 /*or_comma=*/false,
19408 /*consume_paren=*/true);
19409 }
19410 }
19411 else
19412 {
19413 /* We want to record the extra mangling scope for in-class
19414 initializers of class members and initializers of static data
19415 member templates. The former involves deferring
19416 parsing of the initializer until end of class as with default
19417 arguments. So right here we only handle the latter. */
19418 if (!member_p && processing_template_decl)
19419 start_lambda_scope (decl);
19420 initializer = cp_parser_initializer (parser,
19421 &is_direct_init,
19422 &is_non_constant_init);
19423 if (!member_p && processing_template_decl)
19424 finish_lambda_scope ();
19425 if (initializer == error_mark_node)
19426 cp_parser_skip_to_end_of_statement (parser);
19427 }
19428 }
19429
19430 /* The old parser allows attributes to appear after a parenthesized
19431 initializer. Mark Mitchell proposed removing this functionality
19432 on the GCC mailing lists on 2002-08-13. This parser accepts the
19433 attributes -- but ignores them. */
19434 if (cp_parser_allow_gnu_extensions_p (parser)
19435 && initialization_kind == CPP_OPEN_PAREN)
19436 if (cp_parser_attributes_opt (parser))
19437 warning (OPT_Wattributes,
19438 "attributes after parenthesized initializer ignored");
19439
19440 /* And now complain about a non-function implicit template. */
19441 if (bogus_implicit_tmpl && decl != error_mark_node)
19442 error_at (DECL_SOURCE_LOCATION (decl),
19443 "non-function %qD declared as implicit template", decl);
19444
19445 /* For an in-class declaration, use `grokfield' to create the
19446 declaration. */
19447 if (member_p)
19448 {
19449 if (pushed_scope)
19450 {
19451 pop_scope (pushed_scope);
19452 pushed_scope = NULL_TREE;
19453 }
19454 decl = grokfield (declarator, decl_specifiers,
19455 initializer, !is_non_constant_init,
19456 /*asmspec=*/NULL_TREE,
19457 attr_chainon (attributes, prefix_attributes));
19458 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19459 cp_parser_save_default_args (parser, decl);
19460 cp_finalize_omp_declare_simd (parser, decl);
19461 cp_finalize_oacc_routine (parser, decl, false);
19462 }
19463
19464 /* Finish processing the declaration. But, skip member
19465 declarations. */
19466 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19467 {
19468 cp_finish_decl (decl,
19469 initializer, !is_non_constant_init,
19470 asm_specification,
19471 /* If the initializer is in parentheses, then this is
19472 a direct-initialization, which means that an
19473 `explicit' constructor is OK. Otherwise, an
19474 `explicit' constructor cannot be used. */
19475 ((is_direct_init || !is_initialized)
19476 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19477 }
19478 else if ((cxx_dialect != cxx98) && friend_p
19479 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19480 /* Core issue #226 (C++0x only): A default template-argument
19481 shall not be specified in a friend class template
19482 declaration. */
19483 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19484 /*is_partial=*/false, /*is_friend_decl=*/1);
19485
19486 if (!friend_p && pushed_scope)
19487 pop_scope (pushed_scope);
19488
19489 if (function_declarator_p (declarator)
19490 && parser->fully_implicit_function_template_p)
19491 {
19492 if (member_p)
19493 decl = finish_fully_implicit_template (parser, decl);
19494 else
19495 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19496 }
19497
19498 if (auto_result && is_initialized && decl_specifiers->type
19499 && type_uses_auto (decl_specifiers->type))
19500 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19501
19502 return decl;
19503}
19504
19505/* Parse a declarator.
19506
19507 declarator:
19508 direct-declarator
19509 ptr-operator declarator
19510
19511 abstract-declarator:
19512 ptr-operator abstract-declarator [opt]
19513 direct-abstract-declarator
19514
19515 GNU Extensions:
19516
19517 declarator:
19518 attributes [opt] direct-declarator
19519 attributes [opt] ptr-operator declarator
19520
19521 abstract-declarator:
19522 attributes [opt] ptr-operator abstract-declarator [opt]
19523 attributes [opt] direct-abstract-declarator
19524
19525 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19526 detect constructors, destructors, deduction guides, or conversion operators.
19527 It is set to -1 if the declarator is a name, and +1 if it is a
19528 function. Otherwise it is set to zero. Usually you just want to
19529 test for >0, but internally the negative value is used.
19530
19531 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19532 a decl-specifier-seq unless it declares a constructor, destructor,
19533 or conversion. It might seem that we could check this condition in
19534 semantic analysis, rather than parsing, but that makes it difficult
19535 to handle something like `f()'. We want to notice that there are
19536 no decl-specifiers, and therefore realize that this is an
19537 expression, not a declaration.)
19538
19539 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19540 the declarator is a direct-declarator of the form "(...)".
19541
19542 MEMBER_P is true iff this declarator is a member-declarator.
19543
19544 FRIEND_P is true iff this declarator is a friend. */
19545
19546static cp_declarator *
19547cp_parser_declarator (cp_parser* parser,
19548 cp_parser_declarator_kind dcl_kind,
19549 int* ctor_dtor_or_conv_p,
19550 bool* parenthesized_p,
19551 bool member_p, bool friend_p)
19552{
19553 cp_declarator *declarator;
19554 enum tree_code code;
19555 cp_cv_quals cv_quals;
19556 tree class_type;
19557 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19558
19559 /* Assume this is not a constructor, destructor, or type-conversion
19560 operator. */
19561 if (ctor_dtor_or_conv_p)
19562 *ctor_dtor_or_conv_p = 0;
19563
19564 if (cp_parser_allow_gnu_extensions_p (parser))
19565 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19566
19567 /* Check for the ptr-operator production. */
19568 cp_parser_parse_tentatively (parser);
19569 /* Parse the ptr-operator. */
19570 code = cp_parser_ptr_operator (parser,
19571 &class_type,
19572 &cv_quals,
19573 &std_attributes);
19574
19575 /* If that worked, then we have a ptr-operator. */
19576 if (cp_parser_parse_definitely (parser))
19577 {
19578 /* If a ptr-operator was found, then this declarator was not
19579 parenthesized. */
19580 if (parenthesized_p)
19581 *parenthesized_p = true;
19582 /* The dependent declarator is optional if we are parsing an
19583 abstract-declarator. */
19584 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19585 cp_parser_parse_tentatively (parser);
19586
19587 /* Parse the dependent declarator. */
19588 declarator = cp_parser_declarator (parser, dcl_kind,
19589 /*ctor_dtor_or_conv_p=*/NULL,
19590 /*parenthesized_p=*/NULL,
19591 /*member_p=*/false,
19592 friend_p);
19593
19594 /* If we are parsing an abstract-declarator, we must handle the
19595 case where the dependent declarator is absent. */
19596 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19597 && !cp_parser_parse_definitely (parser))
19598 declarator = NULL;
19599
19600 declarator = cp_parser_make_indirect_declarator
19601 (code, class_type, cv_quals, declarator, std_attributes);
19602 }
19603 /* Everything else is a direct-declarator. */
19604 else
19605 {
19606 if (parenthesized_p)
19607 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19608 CPP_OPEN_PAREN);
19609 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19610 ctor_dtor_or_conv_p,
19611 member_p, friend_p);
19612 }
19613
19614 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19615 declarator->attributes = gnu_attributes;
19616 return declarator;
19617}
19618
19619/* Parse a direct-declarator or direct-abstract-declarator.
19620
19621 direct-declarator:
19622 declarator-id
19623 direct-declarator ( parameter-declaration-clause )
19624 cv-qualifier-seq [opt]
19625 ref-qualifier [opt]
19626 exception-specification [opt]
19627 direct-declarator [ constant-expression [opt] ]
19628 ( declarator )
19629
19630 direct-abstract-declarator:
19631 direct-abstract-declarator [opt]
19632 ( parameter-declaration-clause )
19633 cv-qualifier-seq [opt]
19634 ref-qualifier [opt]
19635 exception-specification [opt]
19636 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19637 ( abstract-declarator )
19638
19639 Returns a representation of the declarator. DCL_KIND is
19640 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19641 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19642 we are parsing a direct-declarator. It is
19643 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19644 of ambiguity we prefer an abstract declarator, as per
19645 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19646 as for cp_parser_declarator. */
19647
19648static cp_declarator *
19649cp_parser_direct_declarator (cp_parser* parser,
19650 cp_parser_declarator_kind dcl_kind,
19651 int* ctor_dtor_or_conv_p,
19652 bool member_p, bool friend_p)
19653{
19654 cp_token *token;
19655 cp_declarator *declarator = NULL;
19656 tree scope = NULL_TREE;
19657 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19658 bool saved_in_declarator_p = parser->in_declarator_p;
19659 bool first = true;
19660 tree pushed_scope = NULL_TREE;
19661
19662 while (true)
19663 {
19664 /* Peek at the next token. */
19665 token = cp_lexer_peek_token (parser->lexer);
19666 if (token->type == CPP_OPEN_PAREN)
19667 {
19668 /* This is either a parameter-declaration-clause, or a
19669 parenthesized declarator. When we know we are parsing a
19670 named declarator, it must be a parenthesized declarator
19671 if FIRST is true. For instance, `(int)' is a
19672 parameter-declaration-clause, with an omitted
19673 direct-abstract-declarator. But `((*))', is a
19674 parenthesized abstract declarator. Finally, when T is a
19675 template parameter `(T)' is a
19676 parameter-declaration-clause, and not a parenthesized
19677 named declarator.
19678
19679 We first try and parse a parameter-declaration-clause,
19680 and then try a nested declarator (if FIRST is true).
19681
19682 It is not an error for it not to be a
19683 parameter-declaration-clause, even when FIRST is
19684 false. Consider,
19685
19686 int i (int);
19687 int i (3);
19688
19689 The first is the declaration of a function while the
19690 second is the definition of a variable, including its
19691 initializer.
19692
19693 Having seen only the parenthesis, we cannot know which of
19694 these two alternatives should be selected. Even more
19695 complex are examples like:
19696
19697 int i (int (a));
19698 int i (int (3));
19699
19700 The former is a function-declaration; the latter is a
19701 variable initialization.
19702
19703 Thus again, we try a parameter-declaration-clause, and if
19704 that fails, we back out and return. */
19705
19706 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19707 {
19708 tree params;
19709 bool is_declarator = false;
19710
19711 /* In a member-declarator, the only valid interpretation
19712 of a parenthesis is the start of a
19713 parameter-declaration-clause. (It is invalid to
19714 initialize a static data member with a parenthesized
19715 initializer; only the "=" form of initialization is
19716 permitted.) */
19717 if (!member_p)
19718 cp_parser_parse_tentatively (parser);
19719
19720 /* Consume the `('. */
19721 cp_lexer_consume_token (parser->lexer);
19722 if (first)
19723 {
19724 /* If this is going to be an abstract declarator, we're
19725 in a declarator and we can't have default args. */
19726 parser->default_arg_ok_p = false;
19727 parser->in_declarator_p = true;
19728 }
19729
19730 begin_scope (sk_function_parms, NULL_TREE);
19731
19732 /* Parse the parameter-declaration-clause. */
19733 params = cp_parser_parameter_declaration_clause (parser);
19734
19735 /* Consume the `)'. */
19736 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19737
19738 /* If all went well, parse the cv-qualifier-seq,
19739 ref-qualifier and the exception-specification. */
19740 if (member_p || cp_parser_parse_definitely (parser))
19741 {
19742 cp_cv_quals cv_quals;
19743 cp_virt_specifiers virt_specifiers;
19744 cp_ref_qualifier ref_qual;
19745 tree exception_specification;
19746 tree late_return;
19747 tree attrs;
19748 bool memfn = (member_p || (pushed_scope
19749 && CLASS_TYPE_P (pushed_scope)));
19750
19751 is_declarator = true;
19752
19753 if (ctor_dtor_or_conv_p)
19754 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19755 first = false;
19756
19757 /* Parse the cv-qualifier-seq. */
19758 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19759 /* Parse the ref-qualifier. */
19760 ref_qual = cp_parser_ref_qualifier_opt (parser);
19761 /* Parse the tx-qualifier. */
19762 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19763 /* And the exception-specification. */
19764 exception_specification
19765 = cp_parser_exception_specification_opt (parser);
19766
19767 attrs = cp_parser_std_attribute_spec_seq (parser);
19768
19769 /* In here, we handle cases where attribute is used after
19770 the function declaration. For example:
19771 void func (int x) __attribute__((vector(..))); */
19772 tree gnu_attrs = NULL_TREE;
19773 if (flag_cilkplus
19774 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19775 {
19776 cp_parser_parse_tentatively (parser);
19777 tree attr = cp_parser_gnu_attributes_opt (parser);
19778 if (cp_lexer_next_token_is_not (parser->lexer,
19779 CPP_SEMICOLON)
19780 && cp_lexer_next_token_is_not (parser->lexer,
19781 CPP_OPEN_BRACE))
19782 cp_parser_abort_tentative_parse (parser);
19783 else if (!cp_parser_parse_definitely (parser))
19784 ;
19785 else
19786 gnu_attrs = attr;
19787 }
19788 tree requires_clause = NULL_TREE;
19789 late_return = (cp_parser_late_return_type_opt
19790 (parser, declarator, requires_clause,
19791 memfn ? cv_quals : -1));
19792
19793 /* Parse the virt-specifier-seq. */
19794 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19795
19796 /* Create the function-declarator. */
19797 declarator = make_call_declarator (declarator,
19798 params,
19799 cv_quals,
19800 virt_specifiers,
19801 ref_qual,
19802 tx_qual,
19803 exception_specification,
19804 late_return,
19805 requires_clause);
19806 declarator->std_attributes = attrs;
19807 declarator->attributes = gnu_attrs;
19808 /* Any subsequent parameter lists are to do with
19809 return type, so are not those of the declared
19810 function. */
19811 parser->default_arg_ok_p = false;
19812 }
19813
19814 /* Remove the function parms from scope. */
19815 pop_bindings_and_leave_scope ();
19816
19817 if (is_declarator)
19818 /* Repeat the main loop. */
19819 continue;
19820 }
19821
19822 /* If this is the first, we can try a parenthesized
19823 declarator. */
19824 if (first)
19825 {
19826 bool saved_in_type_id_in_expr_p;
19827
19828 parser->default_arg_ok_p = saved_default_arg_ok_p;
19829 parser->in_declarator_p = saved_in_declarator_p;
19830
19831 /* Consume the `('. */
19832 cp_lexer_consume_token (parser->lexer);
19833 /* Parse the nested declarator. */
19834 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19835 parser->in_type_id_in_expr_p = true;
19836 declarator
19837 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19838 /*parenthesized_p=*/NULL,
19839 member_p, friend_p);
19840 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19841 first = false;
19842 /* Expect a `)'. */
19843 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19844 declarator = cp_error_declarator;
19845 if (declarator == cp_error_declarator)
19846 break;
19847
19848 goto handle_declarator;
19849 }
19850 /* Otherwise, we must be done. */
19851 else
19852 break;
19853 }
19854 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19855 && token->type == CPP_OPEN_SQUARE
19856 && !cp_next_tokens_can_be_attribute_p (parser))
19857 {
19858 /* Parse an array-declarator. */
19859 tree bounds, attrs;
19860
19861 if (ctor_dtor_or_conv_p)
19862 *ctor_dtor_or_conv_p = 0;
19863
19864 first = false;
19865 parser->default_arg_ok_p = false;
19866 parser->in_declarator_p = true;
19867 /* Consume the `['. */
19868 cp_lexer_consume_token (parser->lexer);
19869 /* Peek at the next token. */
19870 token = cp_lexer_peek_token (parser->lexer);
19871 /* If the next token is `]', then there is no
19872 constant-expression. */
19873 if (token->type != CPP_CLOSE_SQUARE)
19874 {
19875 bool non_constant_p;
19876 bounds
19877 = cp_parser_constant_expression (parser,
19878 /*allow_non_constant=*/true,
19879 &non_constant_p);
19880 if (!non_constant_p)
19881 /* OK */;
19882 else if (error_operand_p (bounds))
19883 /* Already gave an error. */;
19884 else if (!parser->in_function_body
19885 || current_binding_level->kind == sk_function_parms)
19886 {
19887 /* Normally, the array bound must be an integral constant
19888 expression. However, as an extension, we allow VLAs
19889 in function scopes as long as they aren't part of a
19890 parameter declaration. */
19891 cp_parser_error (parser,
19892 "array bound is not an integer constant");
19893 bounds = error_mark_node;
19894 }
19895 else if (processing_template_decl
19896 && !type_dependent_expression_p (bounds))
19897 {
19898 /* Remember this wasn't a constant-expression. */
19899 bounds = build_nop (TREE_TYPE (bounds), bounds);
19900 TREE_SIDE_EFFECTS (bounds) = 1;
19901 }
19902 }
19903 else
19904 bounds = NULL_TREE;
19905 /* Look for the closing `]'. */
19906 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19907 {
19908 declarator = cp_error_declarator;
19909 break;
19910 }
19911
19912 attrs = cp_parser_std_attribute_spec_seq (parser);
19913 declarator = make_array_declarator (declarator, bounds);
19914 declarator->std_attributes = attrs;
19915 }
19916 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19917 {
19918 {
19919 tree qualifying_scope;
19920 tree unqualified_name;
19921 tree attrs;
19922 special_function_kind sfk;
19923 bool abstract_ok;
19924 bool pack_expansion_p = false;
19925 cp_token *declarator_id_start_token;
19926
19927 /* Parse a declarator-id */
19928 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19929 if (abstract_ok)
19930 {
19931 cp_parser_parse_tentatively (parser);
19932
19933 /* If we see an ellipsis, we should be looking at a
19934 parameter pack. */
19935 if (token->type == CPP_ELLIPSIS)
19936 {
19937 /* Consume the `...' */
19938 cp_lexer_consume_token (parser->lexer);
19939
19940 pack_expansion_p = true;
19941 }
19942 }
19943
19944 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19945 unqualified_name
19946 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19947 qualifying_scope = parser->scope;
19948 if (abstract_ok)
19949 {
19950 bool okay = false;
19951
19952 if (!unqualified_name && pack_expansion_p)
19953 {
19954 /* Check whether an error occurred. */
19955 okay = !cp_parser_error_occurred (parser);
19956
19957 /* We already consumed the ellipsis to mark a
19958 parameter pack, but we have no way to report it,
19959 so abort the tentative parse. We will be exiting
19960 immediately anyway. */
19961 cp_parser_abort_tentative_parse (parser);
19962 }
19963 else
19964 okay = cp_parser_parse_definitely (parser);
19965
19966 if (!okay)
19967 unqualified_name = error_mark_node;
19968 else if (unqualified_name
19969 && (qualifying_scope
19970 || (!identifier_p (unqualified_name))))
19971 {
19972 cp_parser_error (parser, "expected unqualified-id");
19973 unqualified_name = error_mark_node;
19974 }
19975 }
19976
19977 if (!unqualified_name)
19978 return NULL;
19979 if (unqualified_name == error_mark_node)
19980 {
19981 declarator = cp_error_declarator;
19982 pack_expansion_p = false;
19983 declarator->parameter_pack_p = false;
19984 break;
19985 }
19986
19987 attrs = cp_parser_std_attribute_spec_seq (parser);
19988
19989 if (qualifying_scope && at_namespace_scope_p ()
19990 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19991 {
19992 /* In the declaration of a member of a template class
19993 outside of the class itself, the SCOPE will sometimes
19994 be a TYPENAME_TYPE. For example, given:
19995
19996 template <typename T>
19997 int S<T>::R::i = 3;
19998
19999 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20000 this context, we must resolve S<T>::R to an ordinary
20001 type, rather than a typename type.
20002
20003 The reason we normally avoid resolving TYPENAME_TYPEs
20004 is that a specialization of `S' might render
20005 `S<T>::R' not a type. However, if `S' is
20006 specialized, then this `i' will not be used, so there
20007 is no harm in resolving the types here. */
20008 tree type;
20009
20010 /* Resolve the TYPENAME_TYPE. */
20011 type = resolve_typename_type (qualifying_scope,
20012 /*only_current_p=*/false);
20013 /* If that failed, the declarator is invalid. */
20014 if (TREE_CODE (type) == TYPENAME_TYPE)
20015 {
20016 if (typedef_variant_p (type))
20017 error_at (declarator_id_start_token->location,
20018 "cannot define member of dependent typedef "
20019 "%qT", type);
20020 else
20021 error_at (declarator_id_start_token->location,
20022 "%<%T::%E%> is not a type",
20023 TYPE_CONTEXT (qualifying_scope),
20024 TYPE_IDENTIFIER (qualifying_scope));
20025 }
20026 qualifying_scope = type;
20027 }
20028
20029 sfk = sfk_none;
20030
20031 if (unqualified_name)
20032 {
20033 tree class_type;
20034
20035 if (qualifying_scope
20036 && CLASS_TYPE_P (qualifying_scope))
20037 class_type = qualifying_scope;
20038 else
20039 class_type = current_class_type;
20040
20041 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20042 {
20043 tree name_type = TREE_TYPE (unqualified_name);
20044 if (class_type && same_type_p (name_type, class_type))
20045 {
20046 if (qualifying_scope
20047 && CLASSTYPE_USE_TEMPLATE (name_type))
20048 {
20049 error_at (declarator_id_start_token->location,
20050 "invalid use of constructor as a template");
20051 inform (declarator_id_start_token->location,
20052 "use %<%T::%D%> instead of %<%T::%D%> to "
20053 "name the constructor in a qualified name",
20054 class_type,
20055 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20056 class_type, name_type);
20057 declarator = cp_error_declarator;
20058 break;
20059 }
20060 else
20061 unqualified_name = constructor_name (class_type);
20062 }
20063 else
20064 {
20065 /* We do not attempt to print the declarator
20066 here because we do not have enough
20067 information about its original syntactic
20068 form. */
20069 cp_parser_error (parser, "invalid declarator");
20070 declarator = cp_error_declarator;
20071 break;
20072 }
20073 }
20074
20075 if (class_type)
20076 {
20077 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20078 sfk = sfk_destructor;
20079 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
20080 sfk = sfk_conversion;
20081 else if (/* There's no way to declare a constructor
20082 for an unnamed type, even if the type
20083 got a name for linkage purposes. */
20084 !TYPE_WAS_UNNAMED (class_type)
20085 /* Handle correctly (c++/19200):
20086
20087 struct S {
20088 struct T{};
20089 friend void S(T);
20090 };
20091
20092 and also:
20093
20094 namespace N {
20095 void S();
20096 }
20097
20098 struct S {
20099 friend void N::S();
20100 }; */
20101 && !(friend_p
20102 && class_type != qualifying_scope)
20103 && constructor_name_p (unqualified_name,
20104 class_type))
20105 {
20106 unqualified_name = constructor_name (class_type);
20107 sfk = sfk_constructor;
20108 }
20109 else if (is_overloaded_fn (unqualified_name)
20110 && DECL_CONSTRUCTOR_P (get_first_fn
20111 (unqualified_name)))
20112 sfk = sfk_constructor;
20113
20114 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20115 *ctor_dtor_or_conv_p = -1;
20116 }
20117 }
20118 declarator = make_id_declarator (qualifying_scope,
20119 unqualified_name,
20120 sfk);
20121 declarator->std_attributes = attrs;
20122 declarator->id_loc = token->location;
20123 declarator->parameter_pack_p = pack_expansion_p;
20124
20125 if (pack_expansion_p)
20126 maybe_warn_variadic_templates ();
20127 }
20128
20129 handle_declarator:;
20130 scope = get_scope_of_declarator (declarator);
20131 if (scope)
20132 {
20133 /* Any names that appear after the declarator-id for a
20134 member are looked up in the containing scope. */
20135 if (at_function_scope_p ())
20136 {
20137 /* But declarations with qualified-ids can't appear in a
20138 function. */
20139 cp_parser_error (parser, "qualified-id in declaration");
20140 declarator = cp_error_declarator;
20141 break;
20142 }
20143 pushed_scope = push_scope (scope);
20144 }
20145 parser->in_declarator_p = true;
20146 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20147 || (declarator && declarator->kind == cdk_id))
20148 /* Default args are only allowed on function
20149 declarations. */
20150 parser->default_arg_ok_p = saved_default_arg_ok_p;
20151 else
20152 parser->default_arg_ok_p = false;
20153
20154 first = false;
20155 }
20156 /* We're done. */
20157 else
20158 break;
20159 }
20160
20161 /* For an abstract declarator, we might wind up with nothing at this
20162 point. That's an error; the declarator is not optional. */
20163 if (!declarator)
20164 cp_parser_error (parser, "expected declarator");
20165
20166 /* If we entered a scope, we must exit it now. */
20167 if (pushed_scope)
20168 pop_scope (pushed_scope);
20169
20170 parser->default_arg_ok_p = saved_default_arg_ok_p;
20171 parser->in_declarator_p = saved_in_declarator_p;
20172
20173 return declarator;
20174}
20175
20176/* Parse a ptr-operator.
20177
20178 ptr-operator:
20179 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20180 * cv-qualifier-seq [opt]
20181 &
20182 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20183 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20184
20185 GNU Extension:
20186
20187 ptr-operator:
20188 & cv-qualifier-seq [opt]
20189
20190 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20191 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20192 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20193 filled in with the TYPE containing the member. *CV_QUALS is
20194 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20195 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20196 Note that the tree codes returned by this function have nothing
20197 to do with the types of trees that will be eventually be created
20198 to represent the pointer or reference type being parsed. They are
20199 just constants with suggestive names. */
20200static enum tree_code
20201cp_parser_ptr_operator (cp_parser* parser,
20202 tree* type,
20203 cp_cv_quals *cv_quals,
20204 tree *attributes)
20205{
20206 enum tree_code code = ERROR_MARK;
20207 cp_token *token;
20208 tree attrs = NULL_TREE;
20209
20210 /* Assume that it's not a pointer-to-member. */
20211 *type = NULL_TREE;
20212 /* And that there are no cv-qualifiers. */
20213 *cv_quals = TYPE_UNQUALIFIED;
20214
20215 /* Peek at the next token. */
20216 token = cp_lexer_peek_token (parser->lexer);
20217
20218 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20219 if (token->type == CPP_MULT)
20220 code = INDIRECT_REF;
20221 else if (token->type == CPP_AND)
20222 code = ADDR_EXPR;
20223 else if ((cxx_dialect != cxx98) &&
20224 token->type == CPP_AND_AND) /* C++0x only */
20225 code = NON_LVALUE_EXPR;
20226
20227 if (code != ERROR_MARK)
20228 {
20229 /* Consume the `*', `&' or `&&'. */
20230 cp_lexer_consume_token (parser->lexer);
20231
20232 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20233 `&', if we are allowing GNU extensions. (The only qualifier
20234 that can legally appear after `&' is `restrict', but that is
20235 enforced during semantic analysis. */
20236 if (code == INDIRECT_REF
20237 || cp_parser_allow_gnu_extensions_p (parser))
20238 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20239
20240 attrs = cp_parser_std_attribute_spec_seq (parser);
20241 if (attributes != NULL)
20242 *attributes = attrs;
20243 }
20244 else
20245 {
20246 /* Try the pointer-to-member case. */
20247 cp_parser_parse_tentatively (parser);
20248 /* Look for the optional `::' operator. */
20249 cp_parser_global_scope_opt (parser,
20250 /*current_scope_valid_p=*/false);
20251 /* Look for the nested-name specifier. */
20252 token = cp_lexer_peek_token (parser->lexer);
20253 cp_parser_nested_name_specifier (parser,
20254 /*typename_keyword_p=*/false,
20255 /*check_dependency_p=*/true,
20256 /*type_p=*/false,
20257 /*is_declaration=*/false);
20258 /* If we found it, and the next token is a `*', then we are
20259 indeed looking at a pointer-to-member operator. */
20260 if (!cp_parser_error_occurred (parser)
20261 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20262 {
20263 /* Indicate that the `*' operator was used. */
20264 code = INDIRECT_REF;
20265
20266 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20267 error_at (token->location, "%qD is a namespace", parser->scope);
20268 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20269 error_at (token->location, "cannot form pointer to member of "
20270 "non-class %q#T", parser->scope);
20271 else
20272 {
20273 /* The type of which the member is a member is given by the
20274 current SCOPE. */
20275 *type = parser->scope;
20276 /* The next name will not be qualified. */
20277 parser->scope = NULL_TREE;
20278 parser->qualifying_scope = NULL_TREE;
20279 parser->object_scope = NULL_TREE;
20280 /* Look for optional c++11 attributes. */
20281 attrs = cp_parser_std_attribute_spec_seq (parser);
20282 if (attributes != NULL)
20283 *attributes = attrs;
20284 /* Look for the optional cv-qualifier-seq. */
20285 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20286 }
20287 }
20288 /* If that didn't work we don't have a ptr-operator. */
20289 if (!cp_parser_parse_definitely (parser))
20290 cp_parser_error (parser, "expected ptr-operator");
20291 }
20292
20293 return code;
20294}
20295
20296/* Parse an (optional) cv-qualifier-seq.
20297
20298 cv-qualifier-seq:
20299 cv-qualifier cv-qualifier-seq [opt]
20300
20301 cv-qualifier:
20302 const
20303 volatile
20304
20305 GNU Extension:
20306
20307 cv-qualifier:
20308 __restrict__
20309
20310 Returns a bitmask representing the cv-qualifiers. */
20311
20312static cp_cv_quals
20313cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20314{
20315 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20316
20317 while (true)
20318 {
20319 cp_token *token;
20320 cp_cv_quals cv_qualifier;
20321
20322 /* Peek at the next token. */
20323 token = cp_lexer_peek_token (parser->lexer);
20324 /* See if it's a cv-qualifier. */
20325 switch (token->keyword)
20326 {
20327 case RID_CONST:
20328 cv_qualifier = TYPE_QUAL_CONST;
20329 break;
20330
20331 case RID_VOLATILE:
20332 cv_qualifier = TYPE_QUAL_VOLATILE;
20333 break;
20334
20335 case RID_RESTRICT:
20336 cv_qualifier = TYPE_QUAL_RESTRICT;
20337 break;
20338
20339 default:
20340 cv_qualifier = TYPE_UNQUALIFIED;
20341 break;
20342 }
20343
20344 if (!cv_qualifier)
20345 break;
20346
20347 if (cv_quals & cv_qualifier)
20348 {
20349 error_at (token->location, "duplicate cv-qualifier");
20350 cp_lexer_purge_token (parser->lexer);
20351 }
20352 else
20353 {
20354 cp_lexer_consume_token (parser->lexer);
20355 cv_quals |= cv_qualifier;
20356 }
20357 }
20358
20359 return cv_quals;
20360}
20361
20362/* Parse an (optional) ref-qualifier
20363
20364 ref-qualifier:
20365 &
20366 &&
20367
20368 Returns cp_ref_qualifier representing ref-qualifier. */
20369
20370static cp_ref_qualifier
20371cp_parser_ref_qualifier_opt (cp_parser* parser)
20372{
20373 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20374
20375 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20376 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20377 return ref_qual;
20378
20379 while (true)
20380 {
20381 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20382 cp_token *token = cp_lexer_peek_token (parser->lexer);
20383
20384 switch (token->type)
20385 {
20386 case CPP_AND:
20387 curr_ref_qual = REF_QUAL_LVALUE;
20388 break;
20389
20390 case CPP_AND_AND:
20391 curr_ref_qual = REF_QUAL_RVALUE;
20392 break;
20393
20394 default:
20395 curr_ref_qual = REF_QUAL_NONE;
20396 break;
20397 }
20398
20399 if (!curr_ref_qual)
20400 break;
20401 else if (ref_qual)
20402 {
20403 error_at (token->location, "multiple ref-qualifiers");
20404 cp_lexer_purge_token (parser->lexer);
20405 }
20406 else
20407 {
20408 ref_qual = curr_ref_qual;
20409 cp_lexer_consume_token (parser->lexer);
20410 }
20411 }
20412
20413 return ref_qual;
20414}
20415
20416/* Parse an optional tx-qualifier.
20417
20418 tx-qualifier:
20419 transaction_safe
20420 transaction_safe_dynamic */
20421
20422static tree
20423cp_parser_tx_qualifier_opt (cp_parser *parser)
20424{
20425 cp_token *token = cp_lexer_peek_token (parser->lexer);
20426 if (token->type == CPP_NAME)
20427 {
20428 tree name = token->u.value;
20429 const char *p = IDENTIFIER_POINTER (name);
20430 const int len = strlen ("transaction_safe");
20431 if (!strncmp (p, "transaction_safe", len))
20432 {
20433 p += len;
20434 if (*p == '\0'
20435 || !strcmp (p, "_dynamic"))
20436 {
20437 cp_lexer_consume_token (parser->lexer);
20438 if (!flag_tm)
20439 {
20440 error ("%E requires %<-fgnu-tm%>", name);
20441 return NULL_TREE;
20442 }
20443 else
20444 return name;
20445 }
20446 }
20447 }
20448 return NULL_TREE;
20449}
20450
20451/* Parse an (optional) virt-specifier-seq.
20452
20453 virt-specifier-seq:
20454 virt-specifier virt-specifier-seq [opt]
20455
20456 virt-specifier:
20457 override
20458 final
20459
20460 Returns a bitmask representing the virt-specifiers. */
20461
20462static cp_virt_specifiers
20463cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20464{
20465 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20466
20467 while (true)
20468 {
20469 cp_token *token;
20470 cp_virt_specifiers virt_specifier;
20471
20472 /* Peek at the next token. */
20473 token = cp_lexer_peek_token (parser->lexer);
20474 /* See if it's a virt-specifier-qualifier. */
20475 if (token->type != CPP_NAME)
20476 break;
20477 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20478 {
20479 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20480 virt_specifier = VIRT_SPEC_OVERRIDE;
20481 }
20482 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20483 {
20484 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20485 virt_specifier = VIRT_SPEC_FINAL;
20486 }
20487 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20488 {
20489 virt_specifier = VIRT_SPEC_FINAL;
20490 }
20491 else
20492 break;
20493
20494 if (virt_specifiers & virt_specifier)
20495 {
20496 error_at (token->location, "duplicate virt-specifier");
20497 cp_lexer_purge_token (parser->lexer);
20498 }
20499 else
20500 {
20501 cp_lexer_consume_token (parser->lexer);
20502 virt_specifiers |= virt_specifier;
20503 }
20504 }
20505 return virt_specifiers;
20506}
20507
20508/* Used by handling of trailing-return-types and NSDMI, in which 'this'
20509 is in scope even though it isn't real. */
20510
20511void
20512inject_this_parameter (tree ctype, cp_cv_quals quals)
20513{
20514 tree this_parm;
20515
20516 if (current_class_ptr)
20517 {
20518 /* We don't clear this between NSDMIs. Is it already what we want? */
20519 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20520 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20521 && cp_type_quals (type) == quals)
20522 return;
20523 }
20524
20525 this_parm = build_this_parm (ctype, quals);
20526 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20527 current_class_ptr = NULL_TREE;
20528 current_class_ref
20529 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20530 current_class_ptr = this_parm;
20531}
20532
20533/* Return true iff our current scope is a non-static data member
20534 initializer. */
20535
20536bool
20537parsing_nsdmi (void)
20538{
20539 /* We recognize NSDMI context by the context-less 'this' pointer set up
20540 by the function above. */
20541 if (current_class_ptr
20542 && TREE_CODE (current_class_ptr) == PARM_DECL
20543 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20544 return true;
20545 return false;
20546}
20547
20548/* Return true iff our current scope is a default capturing generic lambda
20549 defined within a template. FIXME: This is part of a workaround (see
20550 semantics.c) to handle building lambda closure types correctly in templates
20551 which we ultimately want to defer to instantiation time. */
20552
20553bool
20554parsing_default_capturing_generic_lambda (void)
20555{
20556 if (!processing_template_decl || !current_class_type)
20557 return false;
20558
20559 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20560 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20561 return false;
20562
20563 tree callop = lambda_function (lam);
20564 if (!callop)
20565 return false;
20566
20567 return generic_lambda_fn_p (callop);
20568}
20569
20570/* Parse a late-specified return type, if any. This is not a separate
20571 non-terminal, but part of a function declarator, which looks like
20572
20573 -> trailing-type-specifier-seq abstract-declarator(opt)
20574
20575 Returns the type indicated by the type-id.
20576
20577 In addition to this, parse any queued up #pragma omp declare simd
20578 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20579 #pragma acc routine clauses.
20580
20581 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20582 function. */
20583
20584static tree
20585cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20586 tree& requires_clause, cp_cv_quals quals)
20587{
20588 cp_token *token;
20589 tree type = NULL_TREE;
20590 bool declare_simd_p = (parser->omp_declare_simd
20591 && declarator
20592 && declarator->kind == cdk_id);
20593
20594 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20595 && declarator && declarator->kind == cdk_id);
20596
20597 bool oacc_routine_p = (parser->oacc_routine
20598 && declarator
20599 && declarator->kind == cdk_id);
20600
20601 /* Peek at the next token. */
20602 token = cp_lexer_peek_token (parser->lexer);
20603 /* A late-specified return type is indicated by an initial '->'. */
20604 if (token->type != CPP_DEREF
20605 && token->keyword != RID_REQUIRES
20606 && !(token->type == CPP_NAME
20607 && token->u.value == ridpointers[RID_REQUIRES])
20608 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20609 return NULL_TREE;
20610
20611 tree save_ccp = current_class_ptr;
20612 tree save_ccr = current_class_ref;
20613 if (quals >= 0)
20614 {
20615 /* DR 1207: 'this' is in scope in the trailing return type. */
20616 inject_this_parameter (current_class_type, quals);
20617 }
20618
20619 if (token->type == CPP_DEREF)
20620 {
20621 /* Consume the ->. */
20622 cp_lexer_consume_token (parser->lexer);
20623
20624 type = cp_parser_trailing_type_id (parser);
20625 }
20626
20627 /* Function declarations may be followed by a trailing
20628 requires-clause. */
20629 requires_clause = cp_parser_requires_clause_opt (parser);
20630
20631 if (cilk_simd_fn_vector_p)
20632 declarator->attributes
20633 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20634 declarator->attributes);
20635 if (declare_simd_p)
20636 declarator->attributes
20637 = cp_parser_late_parsing_omp_declare_simd (parser,
20638 declarator->attributes);
20639 if (oacc_routine_p)
20640 declarator->attributes
20641 = cp_parser_late_parsing_oacc_routine (parser,
20642 declarator->attributes);
20643
20644 if (quals >= 0)
20645 {
20646 current_class_ptr = save_ccp;
20647 current_class_ref = save_ccr;
20648 }
20649
20650 return type;
20651}
20652
20653/* Parse a declarator-id.
20654
20655 declarator-id:
20656 id-expression
20657 :: [opt] nested-name-specifier [opt] type-name
20658
20659 In the `id-expression' case, the value returned is as for
20660 cp_parser_id_expression if the id-expression was an unqualified-id.
20661 If the id-expression was a qualified-id, then a SCOPE_REF is
20662 returned. The first operand is the scope (either a NAMESPACE_DECL
20663 or TREE_TYPE), but the second is still just a representation of an
20664 unqualified-id. */
20665
20666static tree
20667cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20668{
20669 tree id;
20670 /* The expression must be an id-expression. Assume that qualified
20671 names are the names of types so that:
20672
20673 template <class T>
20674 int S<T>::R::i = 3;
20675
20676 will work; we must treat `S<T>::R' as the name of a type.
20677 Similarly, assume that qualified names are templates, where
20678 required, so that:
20679
20680 template <class T>
20681 int S<T>::R<T>::i = 3;
20682
20683 will work, too. */
20684 id = cp_parser_id_expression (parser,
20685 /*template_keyword_p=*/false,
20686 /*check_dependency_p=*/false,
20687 /*template_p=*/NULL,
20688 /*declarator_p=*/true,
20689 optional_p);
20690 if (id && BASELINK_P (id))
20691 id = BASELINK_FUNCTIONS (id);
20692 return id;
20693}
20694
20695/* Parse a type-id.
20696
20697 type-id:
20698 type-specifier-seq abstract-declarator [opt]
20699
20700 Returns the TYPE specified. */
20701
20702static tree
20703cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20704 bool is_trailing_return)
20705{
20706 cp_decl_specifier_seq type_specifier_seq;
20707 cp_declarator *abstract_declarator;
20708
20709 /* Parse the type-specifier-seq. */
20710 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20711 is_trailing_return,
20712 &type_specifier_seq);
20713 if (is_template_arg && type_specifier_seq.type
20714 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20715 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20716 /* A bare template name as a template argument is a template template
20717 argument, not a placeholder, so fail parsing it as a type argument. */
20718 {
20719 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20720 cp_parser_simulate_error (parser);
20721 return error_mark_node;
20722 }
20723 if (type_specifier_seq.type == error_mark_node)
20724 return error_mark_node;
20725
20726 /* There might or might not be an abstract declarator. */
20727 cp_parser_parse_tentatively (parser);
20728 /* Look for the declarator. */
20729 abstract_declarator
20730 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20731 /*parenthesized_p=*/NULL,
20732 /*member_p=*/false,
20733 /*friend_p=*/false);
20734 /* Check to see if there really was a declarator. */
20735 if (!cp_parser_parse_definitely (parser))
20736 abstract_declarator = NULL;
20737
20738 if (type_specifier_seq.type
20739 /* The concepts TS allows 'auto' as a type-id. */
20740 && (!flag_concepts || parser->in_type_id_in_expr_p)
20741 /* None of the valid uses of 'auto' in C++14 involve the type-id
20742 nonterminal, but it is valid in a trailing-return-type. */
20743 && !(cxx_dialect >= cxx14 && is_trailing_return))
20744 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20745 {
20746 /* A type-id with type 'auto' is only ok if the abstract declarator
20747 is a function declarator with a late-specified return type.
20748
20749 A type-id with 'auto' is also valid in a trailing-return-type
20750 in a compound-requirement. */
20751 if (abstract_declarator
20752 && abstract_declarator->kind == cdk_function
20753 && abstract_declarator->u.function.late_return_type)
20754 /* OK */;
20755 else if (parser->in_result_type_constraint_p)
20756 /* OK */;
20757 else
20758 {
20759 location_t loc = type_specifier_seq.locations[ds_type_spec];
20760 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20761 {
20762 error_at (loc, "missing template arguments after %qT",
20763 auto_node);
20764 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20765 tmpl);
20766 }
20767 else
20768 error_at (loc, "invalid use of %qT", auto_node);
20769 return error_mark_node;
20770 }
20771 }
20772
20773 return groktypename (&type_specifier_seq, abstract_declarator,
20774 is_template_arg);
20775}
20776
20777static tree
20778cp_parser_type_id (cp_parser *parser)
20779{
20780 return cp_parser_type_id_1 (parser, false, false);
20781}
20782
20783static tree
20784cp_parser_template_type_arg (cp_parser *parser)
20785{
20786 tree r;
20787 const char *saved_message = parser->type_definition_forbidden_message;
20788 parser->type_definition_forbidden_message
20789 = G_("types may not be defined in template arguments");
20790 r = cp_parser_type_id_1 (parser, true, false);
20791 parser->type_definition_forbidden_message = saved_message;
20792 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20793 {
20794 error ("invalid use of %<auto%> in template argument");
20795 r = error_mark_node;
20796 }
20797 return r;
20798}
20799
20800static tree
20801cp_parser_trailing_type_id (cp_parser *parser)
20802{
20803 return cp_parser_type_id_1 (parser, false, true);
20804}
20805
20806/* Parse a type-specifier-seq.
20807
20808 type-specifier-seq:
20809 type-specifier type-specifier-seq [opt]
20810
20811 GNU extension:
20812
20813 type-specifier-seq:
20814 attributes type-specifier-seq [opt]
20815
20816 If IS_DECLARATION is true, we are at the start of a "condition" or
20817 exception-declaration, so we might be followed by a declarator-id.
20818
20819 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20820 i.e. we've just seen "->".
20821
20822 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20823
20824static void
20825cp_parser_type_specifier_seq (cp_parser* parser,
20826 bool is_declaration,
20827 bool is_trailing_return,
20828 cp_decl_specifier_seq *type_specifier_seq)
20829{
20830 bool seen_type_specifier = false;
20831 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20832 cp_token *start_token = NULL;
20833
20834 /* Clear the TYPE_SPECIFIER_SEQ. */
20835 clear_decl_specs (type_specifier_seq);
20836
20837 /* In the context of a trailing return type, enum E { } is an
20838 elaborated-type-specifier followed by a function-body, not an
20839 enum-specifier. */
20840 if (is_trailing_return)
20841 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20842
20843 /* Parse the type-specifiers and attributes. */
20844 while (true)
20845 {
20846 tree type_specifier;
20847 bool is_cv_qualifier;
20848
20849 /* Check for attributes first. */
20850 if (cp_next_tokens_can_be_attribute_p (parser))
20851 {
20852 type_specifier_seq->attributes
20853 = attr_chainon (type_specifier_seq->attributes,
20854 cp_parser_attributes_opt (parser));
20855 continue;
20856 }
20857
20858 /* record the token of the beginning of the type specifier seq,
20859 for error reporting purposes*/
20860 if (!start_token)
20861 start_token = cp_lexer_peek_token (parser->lexer);
20862
20863 /* Look for the type-specifier. */
20864 type_specifier = cp_parser_type_specifier (parser,
20865 flags,
20866 type_specifier_seq,
20867 /*is_declaration=*/false,
20868 NULL,
20869 &is_cv_qualifier);
20870 if (!type_specifier)
20871 {
20872 /* If the first type-specifier could not be found, this is not a
20873 type-specifier-seq at all. */
20874 if (!seen_type_specifier)
20875 {
20876 /* Set in_declarator_p to avoid skipping to the semicolon. */
20877 int in_decl = parser->in_declarator_p;
20878 parser->in_declarator_p = true;
20879
20880 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20881 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20882 cp_parser_error (parser, "expected type-specifier");
20883
20884 parser->in_declarator_p = in_decl;
20885
20886 type_specifier_seq->type = error_mark_node;
20887 return;
20888 }
20889 /* If subsequent type-specifiers could not be found, the
20890 type-specifier-seq is complete. */
20891 break;
20892 }
20893
20894 seen_type_specifier = true;
20895 /* The standard says that a condition can be:
20896
20897 type-specifier-seq declarator = assignment-expression
20898
20899 However, given:
20900
20901 struct S {};
20902 if (int S = ...)
20903
20904 we should treat the "S" as a declarator, not as a
20905 type-specifier. The standard doesn't say that explicitly for
20906 type-specifier-seq, but it does say that for
20907 decl-specifier-seq in an ordinary declaration. Perhaps it
20908 would be clearer just to allow a decl-specifier-seq here, and
20909 then add a semantic restriction that if any decl-specifiers
20910 that are not type-specifiers appear, the program is invalid. */
20911 if (is_declaration && !is_cv_qualifier)
20912 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20913 }
20914}
20915
20916/* Return whether the function currently being declared has an associated
20917 template parameter list. */
20918
20919static bool
20920function_being_declared_is_template_p (cp_parser* parser)
20921{
20922 if (!current_template_parms || processing_template_parmlist)
20923 return false;
20924
20925 if (parser->implicit_template_scope)
20926 return true;
20927
20928 if (at_class_scope_p ()
20929 && TYPE_BEING_DEFINED (current_class_type))
20930 return parser->num_template_parameter_lists != 0;
20931
20932 return ((int) parser->num_template_parameter_lists > template_class_depth
20933 (current_class_type));
20934}
20935
20936/* Parse a parameter-declaration-clause.
20937
20938 parameter-declaration-clause:
20939 parameter-declaration-list [opt] ... [opt]
20940 parameter-declaration-list , ...
20941
20942 Returns a representation for the parameter declarations. A return
20943 value of NULL indicates a parameter-declaration-clause consisting
20944 only of an ellipsis. */
20945
20946static tree
20947cp_parser_parameter_declaration_clause (cp_parser* parser)
20948{
20949 tree parameters;
20950 cp_token *token;
20951 bool ellipsis_p;
20952 bool is_error;
20953
20954 struct cleanup {
20955 cp_parser* parser;
20956 int auto_is_implicit_function_template_parm_p;
20957 ~cleanup() {
20958 parser->auto_is_implicit_function_template_parm_p
20959 = auto_is_implicit_function_template_parm_p;
20960 }
20961 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20962
20963 (void) cleanup;
20964
20965 if (!processing_specialization
20966 && !processing_template_parmlist
20967 && !processing_explicit_instantiation
20968 /* default_arg_ok_p tracks whether this is a parameter-clause for an
20969 actual function or a random abstract declarator. */
20970 && parser->default_arg_ok_p)
20971 if (!current_function_decl
20972 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20973 parser->auto_is_implicit_function_template_parm_p = true;
20974
20975 /* Peek at the next token. */
20976 token = cp_lexer_peek_token (parser->lexer);
20977 /* Check for trivial parameter-declaration-clauses. */
20978 if (token->type == CPP_ELLIPSIS)
20979 {
20980 /* Consume the `...' token. */
20981 cp_lexer_consume_token (parser->lexer);
20982 return NULL_TREE;
20983 }
20984 else if (token->type == CPP_CLOSE_PAREN)
20985 /* There are no parameters. */
20986 {
20987#ifndef NO_IMPLICIT_EXTERN_C
20988 if (in_system_header_at (input_location)
20989 && current_class_type == NULL
20990 && current_lang_name == lang_name_c)
20991 return NULL_TREE;
20992 else
20993#endif
20994 return void_list_node;
20995 }
20996 /* Check for `(void)', too, which is a special case. */
20997 else if (token->keyword == RID_VOID
20998 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20999 == CPP_CLOSE_PAREN))
21000 {
21001 /* Consume the `void' token. */
21002 cp_lexer_consume_token (parser->lexer);
21003 /* There are no parameters. */
21004 return void_list_node;
21005 }
21006
21007 /* Parse the parameter-declaration-list. */
21008 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21009 /* If a parse error occurred while parsing the
21010 parameter-declaration-list, then the entire
21011 parameter-declaration-clause is erroneous. */
21012 if (is_error)
21013 return NULL;
21014
21015 /* Peek at the next token. */
21016 token = cp_lexer_peek_token (parser->lexer);
21017 /* If it's a `,', the clause should terminate with an ellipsis. */
21018 if (token->type == CPP_COMMA)
21019 {
21020 /* Consume the `,'. */
21021 cp_lexer_consume_token (parser->lexer);
21022 /* Expect an ellipsis. */
21023 ellipsis_p
21024 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21025 }
21026 /* It might also be `...' if the optional trailing `,' was
21027 omitted. */
21028 else if (token->type == CPP_ELLIPSIS)
21029 {
21030 /* Consume the `...' token. */
21031 cp_lexer_consume_token (parser->lexer);
21032 /* And remember that we saw it. */
21033 ellipsis_p = true;
21034 }
21035 else
21036 ellipsis_p = false;
21037
21038 /* Finish the parameter list. */
21039 if (!ellipsis_p)
21040 parameters = chainon (parameters, void_list_node);
21041
21042 return parameters;
21043}
21044
21045/* Parse a parameter-declaration-list.
21046
21047 parameter-declaration-list:
21048 parameter-declaration
21049 parameter-declaration-list , parameter-declaration
21050
21051 Returns a representation of the parameter-declaration-list, as for
21052 cp_parser_parameter_declaration_clause. However, the
21053 `void_list_node' is never appended to the list. Upon return,
21054 *IS_ERROR will be true iff an error occurred. */
21055
21056static tree
21057cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21058{
21059 tree parameters = NULL_TREE;
21060 tree *tail = &parameters;
21061 bool saved_in_unbraced_linkage_specification_p;
21062 int index = 0;
21063
21064 /* Assume all will go well. */
21065 *is_error = false;
21066 /* The special considerations that apply to a function within an
21067 unbraced linkage specifications do not apply to the parameters
21068 to the function. */
21069 saved_in_unbraced_linkage_specification_p
21070 = parser->in_unbraced_linkage_specification_p;
21071 parser->in_unbraced_linkage_specification_p = false;
21072
21073 /* Look for more parameters. */
21074 while (true)
21075 {
21076 cp_parameter_declarator *parameter;
21077 tree decl = error_mark_node;
21078 bool parenthesized_p = false;
21079
21080 /* Parse the parameter. */
21081 parameter
21082 = cp_parser_parameter_declaration (parser,
21083 /*template_parm_p=*/false,
21084 &parenthesized_p);
21085
21086 /* We don't know yet if the enclosing context is deprecated, so wait
21087 and warn in grokparms if appropriate. */
21088 deprecated_state = DEPRECATED_SUPPRESS;
21089
21090 if (parameter)
21091 {
21092 decl = grokdeclarator (parameter->declarator,
21093 &parameter->decl_specifiers,
21094 PARM,
21095 parameter->default_argument != NULL_TREE,
21096 &parameter->decl_specifiers.attributes);
21097 }
21098
21099 deprecated_state = DEPRECATED_NORMAL;
21100
21101 /* If a parse error occurred parsing the parameter declaration,
21102 then the entire parameter-declaration-list is erroneous. */
21103 if (decl == error_mark_node)
21104 {
21105 *is_error = true;
21106 parameters = error_mark_node;
21107 break;
21108 }
21109
21110 if (parameter->decl_specifiers.attributes)
21111 cplus_decl_attributes (&decl,
21112 parameter->decl_specifiers.attributes,
21113 0);
21114 if (DECL_NAME (decl))
21115 decl = pushdecl (decl);
21116
21117 if (decl != error_mark_node)
21118 {
21119 retrofit_lang_decl (decl);
21120 DECL_PARM_INDEX (decl) = ++index;
21121 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21122 }
21123
21124 /* Add the new parameter to the list. */
21125 *tail = build_tree_list (parameter->default_argument, decl);
21126 tail = &TREE_CHAIN (*tail);
21127
21128 /* Peek at the next token. */
21129 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21130 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21131 /* These are for Objective-C++ */
21132 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21133 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21134 /* The parameter-declaration-list is complete. */
21135 break;
21136 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21137 {
21138 cp_token *token;
21139
21140 /* Peek at the next token. */
21141 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21142 /* If it's an ellipsis, then the list is complete. */
21143 if (token->type == CPP_ELLIPSIS)
21144 break;
21145 /* Otherwise, there must be more parameters. Consume the
21146 `,'. */
21147 cp_lexer_consume_token (parser->lexer);
21148 /* When parsing something like:
21149
21150 int i(float f, double d)
21151
21152 we can tell after seeing the declaration for "f" that we
21153 are not looking at an initialization of a variable "i",
21154 but rather at the declaration of a function "i".
21155
21156 Due to the fact that the parsing of template arguments
21157 (as specified to a template-id) requires backtracking we
21158 cannot use this technique when inside a template argument
21159 list. */
21160 if (!parser->in_template_argument_list_p
21161 && !parser->in_type_id_in_expr_p
21162 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21163 /* However, a parameter-declaration of the form
21164 "float(f)" (which is a valid declaration of a
21165 parameter "f") can also be interpreted as an
21166 expression (the conversion of "f" to "float"). */
21167 && !parenthesized_p)
21168 cp_parser_commit_to_tentative_parse (parser);
21169 }
21170 else
21171 {
21172 cp_parser_error (parser, "expected %<,%> or %<...%>");
21173 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21174 cp_parser_skip_to_closing_parenthesis (parser,
21175 /*recovering=*/true,
21176 /*or_comma=*/false,
21177 /*consume_paren=*/false);
21178 break;
21179 }
21180 }
21181
21182 parser->in_unbraced_linkage_specification_p
21183 = saved_in_unbraced_linkage_specification_p;
21184
21185 /* Reset implicit_template_scope if we are about to leave the function
21186 parameter list that introduced it. Note that for out-of-line member
21187 definitions, there will be one or more class scopes before we get to
21188 the template parameter scope. */
21189
21190 if (cp_binding_level *its = parser->implicit_template_scope)
21191 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21192 {
21193 while (maybe_its->kind == sk_class)
21194 maybe_its = maybe_its->level_chain;
21195 if (maybe_its == its)
21196 {
21197 parser->implicit_template_parms = 0;
21198 parser->implicit_template_scope = 0;
21199 }
21200 }
21201
21202 return parameters;
21203}
21204
21205/* Parse a parameter declaration.
21206
21207 parameter-declaration:
21208 decl-specifier-seq ... [opt] declarator
21209 decl-specifier-seq declarator = assignment-expression
21210 decl-specifier-seq ... [opt] abstract-declarator [opt]
21211 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21212
21213 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21214 declares a template parameter. (In that case, a non-nested `>'
21215 token encountered during the parsing of the assignment-expression
21216 is not interpreted as a greater-than operator.)
21217
21218 Returns a representation of the parameter, or NULL if an error
21219 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21220 true iff the declarator is of the form "(p)". */
21221
21222static cp_parameter_declarator *
21223cp_parser_parameter_declaration (cp_parser *parser,
21224 bool template_parm_p,
21225 bool *parenthesized_p)
21226{
21227 int declares_class_or_enum;
21228 cp_decl_specifier_seq decl_specifiers;
21229 cp_declarator *declarator;
21230 tree default_argument;
21231 cp_token *token = NULL, *declarator_token_start = NULL;
21232 const char *saved_message;
21233 bool template_parameter_pack_p = false;
21234
21235 /* In a template parameter, `>' is not an operator.
21236
21237 [temp.param]
21238
21239 When parsing a default template-argument for a non-type
21240 template-parameter, the first non-nested `>' is taken as the end
21241 of the template parameter-list rather than a greater-than
21242 operator. */
21243
21244 /* Type definitions may not appear in parameter types. */
21245 saved_message = parser->type_definition_forbidden_message;
21246 parser->type_definition_forbidden_message
21247 = G_("types may not be defined in parameter types");
21248
21249 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21250 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21251 (current_template_parms)) : 0);
21252
21253 /* Parse the declaration-specifiers. */
21254 cp_parser_decl_specifier_seq (parser,
21255 CP_PARSER_FLAGS_NONE,
21256 &decl_specifiers,
21257 &declares_class_or_enum);
21258
21259 /* Complain about missing 'typename' or other invalid type names. */
21260 if (!decl_specifiers.any_type_specifiers_p
21261 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21262 decl_specifiers.type = error_mark_node;
21263
21264 /* If an error occurred, there's no reason to attempt to parse the
21265 rest of the declaration. */
21266 if (cp_parser_error_occurred (parser))
21267 {
21268 parser->type_definition_forbidden_message = saved_message;
21269 return NULL;
21270 }
21271
21272 /* Peek at the next token. */
21273 token = cp_lexer_peek_token (parser->lexer);
21274
21275 /* If the next token is a `)', `,', `=', `>', or `...', then there
21276 is no declarator. However, when variadic templates are enabled,
21277 there may be a declarator following `...'. */
21278 if (token->type == CPP_CLOSE_PAREN
21279 || token->type == CPP_COMMA
21280 || token->type == CPP_EQ
21281 || token->type == CPP_GREATER)
21282 {
21283 declarator = NULL;
21284 if (parenthesized_p)
21285 *parenthesized_p = false;
21286 }
21287 /* Otherwise, there should be a declarator. */
21288 else
21289 {
21290 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21291 parser->default_arg_ok_p = false;
21292
21293 /* After seeing a decl-specifier-seq, if the next token is not a
21294 "(", there is no possibility that the code is a valid
21295 expression. Therefore, if parsing tentatively, we commit at
21296 this point. */
21297 if (!parser->in_template_argument_list_p
21298 /* In an expression context, having seen:
21299
21300 (int((char ...
21301
21302 we cannot be sure whether we are looking at a
21303 function-type (taking a "char" as a parameter) or a cast
21304 of some object of type "char" to "int". */
21305 && !parser->in_type_id_in_expr_p
21306 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21307 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21308 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21309 cp_parser_commit_to_tentative_parse (parser);
21310 /* Parse the declarator. */
21311 declarator_token_start = token;
21312 declarator = cp_parser_declarator (parser,
21313 CP_PARSER_DECLARATOR_EITHER,
21314 /*ctor_dtor_or_conv_p=*/NULL,
21315 parenthesized_p,
21316 /*member_p=*/false,
21317 /*friend_p=*/false);
21318 parser->default_arg_ok_p = saved_default_arg_ok_p;
21319 /* After the declarator, allow more attributes. */
21320 decl_specifiers.attributes
21321 = attr_chainon (decl_specifiers.attributes,
21322 cp_parser_attributes_opt (parser));
21323
21324 /* If the declarator is a template parameter pack, remember that and
21325 clear the flag in the declarator itself so we don't get errors
21326 from grokdeclarator. */
21327 if (template_parm_p && declarator && declarator->parameter_pack_p)
21328 {
21329 declarator->parameter_pack_p = false;
21330 template_parameter_pack_p = true;
21331 }
21332 }
21333
21334 /* If the next token is an ellipsis, and we have not seen a declarator
21335 name, and if either the type of the declarator contains parameter
21336 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21337 for, eg, abbreviated integral type names), then we actually have a
21338 parameter pack expansion expression. Otherwise, leave the ellipsis
21339 for a C-style variadic function. */
21340 token = cp_lexer_peek_token (parser->lexer);
21341
21342 /* If a function parameter pack was specified and an implicit template
21343 parameter was introduced during cp_parser_parameter_declaration,
21344 change any implicit parameters introduced into packs. */
21345 if (parser->implicit_template_parms
21346 && ((token->type == CPP_ELLIPSIS
21347 && declarator_can_be_parameter_pack (declarator))
21348 || (declarator && declarator->parameter_pack_p)))
21349 {
21350 int latest_template_parm_idx = TREE_VEC_LENGTH
21351 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21352
21353 if (latest_template_parm_idx != template_parm_idx)
21354 decl_specifiers.type = convert_generic_types_to_packs
21355 (decl_specifiers.type,
21356 template_parm_idx, latest_template_parm_idx);
21357 }
21358
21359 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21360 {
21361 tree type = decl_specifiers.type;
21362
21363 if (type && DECL_P (type))
21364 type = TREE_TYPE (type);
21365
21366 if (((type
21367 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21368 && (template_parm_p || uses_parameter_packs (type)))
21369 || (!type && template_parm_p))
21370 && declarator_can_be_parameter_pack (declarator))
21371 {
21372 /* Consume the `...'. */
21373 cp_lexer_consume_token (parser->lexer);
21374 maybe_warn_variadic_templates ();
21375
21376 /* Build a pack expansion type */
21377 if (template_parm_p)
21378 template_parameter_pack_p = true;
21379 else if (declarator)
21380 declarator->parameter_pack_p = true;
21381 else
21382 decl_specifiers.type = make_pack_expansion (type);
21383 }
21384 }
21385
21386 /* The restriction on defining new types applies only to the type
21387 of the parameter, not to the default argument. */
21388 parser->type_definition_forbidden_message = saved_message;
21389
21390 /* If the next token is `=', then process a default argument. */
21391 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21392 {
21393 tree type = decl_specifiers.type;
21394 token = cp_lexer_peek_token (parser->lexer);
21395 /* If we are defining a class, then the tokens that make up the
21396 default argument must be saved and processed later. */
21397 if (!template_parm_p && at_class_scope_p ()
21398 && TYPE_BEING_DEFINED (current_class_type)
21399 && !LAMBDA_TYPE_P (current_class_type))
21400 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21401
21402 // A constrained-type-specifier may declare a type template-parameter.
21403 else if (declares_constrained_type_template_parameter (type))
21404 default_argument
21405 = cp_parser_default_type_template_argument (parser);
21406
21407 // A constrained-type-specifier may declare a template-template-parameter.
21408 else if (declares_constrained_template_template_parameter (type))
21409 default_argument
21410 = cp_parser_default_template_template_argument (parser);
21411
21412 /* Outside of a class definition, we can just parse the
21413 assignment-expression. */
21414 else
21415 default_argument
21416 = cp_parser_default_argument (parser, template_parm_p);
21417
21418 if (!parser->default_arg_ok_p)
21419 {
21420 permerror (token->location,
21421 "default arguments are only "
21422 "permitted for function parameters");
21423 }
21424 else if ((declarator && declarator->parameter_pack_p)
21425 || template_parameter_pack_p
21426 || (decl_specifiers.type
21427 && PACK_EXPANSION_P (decl_specifiers.type)))
21428 {
21429 /* Find the name of the parameter pack. */
21430 cp_declarator *id_declarator = declarator;
21431 while (id_declarator && id_declarator->kind != cdk_id)
21432 id_declarator = id_declarator->declarator;
21433
21434 if (id_declarator && id_declarator->kind == cdk_id)
21435 error_at (declarator_token_start->location,
21436 template_parm_p
21437 ? G_("template parameter pack %qD "
21438 "cannot have a default argument")
21439 : G_("parameter pack %qD cannot have "
21440 "a default argument"),
21441 id_declarator->u.id.unqualified_name);
21442 else
21443 error_at (declarator_token_start->location,
21444 template_parm_p
21445 ? G_("template parameter pack cannot have "
21446 "a default argument")
21447 : G_("parameter pack cannot have a "
21448 "default argument"));
21449
21450 default_argument = NULL_TREE;
21451 }
21452 }
21453 else
21454 default_argument = NULL_TREE;
21455
21456 return make_parameter_declarator (&decl_specifiers,
21457 declarator,
21458 default_argument,
21459 template_parameter_pack_p);
21460}
21461
21462/* Parse a default argument and return it.
21463
21464 TEMPLATE_PARM_P is true if this is a default argument for a
21465 non-type template parameter. */
21466static tree
21467cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21468{
21469 tree default_argument = NULL_TREE;
21470 bool saved_greater_than_is_operator_p;
21471 bool saved_local_variables_forbidden_p;
21472 bool non_constant_p, is_direct_init;
21473
21474 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21475 set correctly. */
21476 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21477 parser->greater_than_is_operator_p = !template_parm_p;
21478 /* Local variable names (and the `this' keyword) may not
21479 appear in a default argument. */
21480 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21481 parser->local_variables_forbidden_p = true;
21482 /* Parse the assignment-expression. */
21483 if (template_parm_p)
21484 push_deferring_access_checks (dk_no_deferred);
21485 tree saved_class_ptr = NULL_TREE;
21486 tree saved_class_ref = NULL_TREE;
21487 /* The "this" pointer is not valid in a default argument. */
21488 if (cfun)
21489 {
21490 saved_class_ptr = current_class_ptr;
21491 cp_function_chain->x_current_class_ptr = NULL_TREE;
21492 saved_class_ref = current_class_ref;
21493 cp_function_chain->x_current_class_ref = NULL_TREE;
21494 }
21495 default_argument
21496 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21497 /* Restore the "this" pointer. */
21498 if (cfun)
21499 {
21500 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21501 cp_function_chain->x_current_class_ref = saved_class_ref;
21502 }
21503 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21505 if (template_parm_p)
21506 pop_deferring_access_checks ();
21507 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21508 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21509
21510 return default_argument;
21511}
21512
21513/* Parse a function-body.
21514
21515 function-body:
21516 compound_statement */
21517
21518static void
21519cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21520{
21521 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21522 ? BCS_TRY_BLOCK : BCS_NORMAL),
21523 true);
21524}
21525
21526/* Parse a ctor-initializer-opt followed by a function-body. Return
21527 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21528 is true we are parsing a function-try-block. */
21529
21530static bool
21531cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21532 bool in_function_try_block)
21533{
21534 tree body, list;
21535 bool ctor_initializer_p;
21536 const bool check_body_p =
21537 DECL_CONSTRUCTOR_P (current_function_decl)
21538 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21539 tree last = NULL;
21540
21541 /* Begin the function body. */
21542 body = begin_function_body ();
21543 /* Parse the optional ctor-initializer. */
21544 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21545
21546 /* If we're parsing a constexpr constructor definition, we need
21547 to check that the constructor body is indeed empty. However,
21548 before we get to cp_parser_function_body lot of junk has been
21549 generated, so we can't just check that we have an empty block.
21550 Rather we take a snapshot of the outermost block, and check whether
21551 cp_parser_function_body changed its state. */
21552 if (check_body_p)
21553 {
21554 list = cur_stmt_list;
21555 if (STATEMENT_LIST_TAIL (list))
21556 last = STATEMENT_LIST_TAIL (list)->stmt;
21557 }
21558 /* Parse the function-body. */
21559 cp_parser_function_body (parser, in_function_try_block);
21560 if (check_body_p)
21561 check_constexpr_ctor_body (last, list, /*complain=*/true);
21562 /* Finish the function body. */
21563 finish_function_body (body);
21564
21565 return ctor_initializer_p;
21566}
21567
21568/* Parse an initializer.
21569
21570 initializer:
21571 = initializer-clause
21572 ( expression-list )
21573
21574 Returns an expression representing the initializer. If no
21575 initializer is present, NULL_TREE is returned.
21576
21577 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21578 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21579 set to TRUE if there is no initializer present. If there is an
21580 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21581 is set to true; otherwise it is set to false. */
21582
21583static tree
21584cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21585 bool* non_constant_p)
21586{
21587 cp_token *token;
21588 tree init;
21589
21590 /* Peek at the next token. */
21591 token = cp_lexer_peek_token (parser->lexer);
21592
21593 /* Let our caller know whether or not this initializer was
21594 parenthesized. */
21595 *is_direct_init = (token->type != CPP_EQ);
21596 /* Assume that the initializer is constant. */
21597 *non_constant_p = false;
21598
21599 if (token->type == CPP_EQ)
21600 {
21601 /* Consume the `='. */
21602 cp_lexer_consume_token (parser->lexer);
21603 /* Parse the initializer-clause. */
21604 init = cp_parser_initializer_clause (parser, non_constant_p);
21605 }
21606 else if (token->type == CPP_OPEN_PAREN)
21607 {
21608 vec<tree, va_gc> *vec;
21609 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21610 /*cast_p=*/false,
21611 /*allow_expansion_p=*/true,
21612 non_constant_p);
21613 if (vec == NULL)
21614 return error_mark_node;
21615 init = build_tree_list_vec (vec);
21616 release_tree_vector (vec);
21617 }
21618 else if (token->type == CPP_OPEN_BRACE)
21619 {
21620 cp_lexer_set_source_position (parser->lexer);
21621 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21622 init = cp_parser_braced_list (parser, non_constant_p);
21623 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21624 }
21625 else
21626 {
21627 /* Anything else is an error. */
21628 cp_parser_error (parser, "expected initializer");
21629 init = error_mark_node;
21630 }
21631
21632 if (check_for_bare_parameter_packs (init))
21633 init = error_mark_node;
21634
21635 return init;
21636}
21637
21638/* Parse an initializer-clause.
21639
21640 initializer-clause:
21641 assignment-expression
21642 braced-init-list
21643
21644 Returns an expression representing the initializer.
21645
21646 If the `assignment-expression' production is used the value
21647 returned is simply a representation for the expression.
21648
21649 Otherwise, calls cp_parser_braced_list. */
21650
21651static cp_expr
21652cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21653{
21654 cp_expr initializer;
21655
21656 /* Assume the expression is constant. */
21657 *non_constant_p = false;
21658
21659 /* If it is not a `{', then we are looking at an
21660 assignment-expression. */
21661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21662 {
21663 initializer
21664 = cp_parser_constant_expression (parser,
21665 /*allow_non_constant_p=*/true,
21666 non_constant_p);
21667 }
21668 else
21669 initializer = cp_parser_braced_list (parser, non_constant_p);
21670
21671 return initializer;
21672}
21673
21674/* Parse a brace-enclosed initializer list.
21675
21676 braced-init-list:
21677 { initializer-list , [opt] }
21678 { }
21679
21680 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21681 the elements of the initializer-list (or NULL, if the last
21682 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21683 NULL_TREE. There is no way to detect whether or not the optional
21684 trailing `,' was provided. NON_CONSTANT_P is as for
21685 cp_parser_initializer. */
21686
21687static cp_expr
21688cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21689{
21690 tree initializer;
21691 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21692
21693 /* Consume the `{' token. */
21694 cp_lexer_consume_token (parser->lexer);
21695 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21696 initializer = make_node (CONSTRUCTOR);
21697 /* If it's not a `}', then there is a non-trivial initializer. */
21698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21699 {
21700 /* Parse the initializer list. */
21701 CONSTRUCTOR_ELTS (initializer)
21702 = cp_parser_initializer_list (parser, non_constant_p);
21703 /* A trailing `,' token is allowed. */
21704 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21705 cp_lexer_consume_token (parser->lexer);
21706 }
21707 else
21708 *non_constant_p = false;
21709 /* Now, there should be a trailing `}'. */
21710 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21711 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21712 TREE_TYPE (initializer) = init_list_type_node;
21713
21714 cp_expr result (initializer);
21715 /* Build a location of the form:
21716 { ... }
21717 ^~~~~~~
21718 with caret==start at the open brace, finish at the close brace. */
21719 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21720 result.set_location (combined_loc);
21721 return result;
21722}
21723
21724/* Consume tokens up to, and including, the next non-nested closing `]'.
21725 Returns true iff we found a closing `]'. */
21726
21727static bool
21728cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21729{
21730 unsigned square_depth = 0;
21731
21732 while (true)
21733 {
21734 cp_token * token = cp_lexer_peek_token (parser->lexer);
21735
21736 switch (token->type)
21737 {
21738 case CPP_EOF:
21739 case CPP_PRAGMA_EOL:
21740 /* If we've run out of tokens, then there is no closing `]'. */
21741 return false;
21742
21743 case CPP_OPEN_SQUARE:
21744 ++square_depth;
21745 break;
21746
21747 case CPP_CLOSE_SQUARE:
21748 if (!square_depth--)
21749 {
21750 cp_lexer_consume_token (parser->lexer);
21751 return true;
21752 }
21753 break;
21754
21755 default:
21756 break;
21757 }
21758
21759 /* Consume the token. */
21760 cp_lexer_consume_token (parser->lexer);
21761 }
21762}
21763
21764/* Return true if we are looking at an array-designator, false otherwise. */
21765
21766static bool
21767cp_parser_array_designator_p (cp_parser *parser)
21768{
21769 /* Consume the `['. */
21770 cp_lexer_consume_token (parser->lexer);
21771
21772 cp_lexer_save_tokens (parser->lexer);
21773
21774 /* Skip tokens until the next token is a closing square bracket.
21775 If we find the closing `]', and the next token is a `=', then
21776 we are looking at an array designator. */
21777 bool array_designator_p
21778 = (cp_parser_skip_to_closing_square_bracket (parser)
21779 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21780
21781 /* Roll back the tokens we skipped. */
21782 cp_lexer_rollback_tokens (parser->lexer);
21783
21784 return array_designator_p;
21785}
21786
21787/* Parse an initializer-list.
21788
21789 initializer-list:
21790 initializer-clause ... [opt]
21791 initializer-list , initializer-clause ... [opt]
21792
21793 GNU Extension:
21794
21795 initializer-list:
21796 designation initializer-clause ...[opt]
21797 initializer-list , designation initializer-clause ...[opt]
21798
21799 designation:
21800 . identifier =
21801 identifier :
21802 [ constant-expression ] =
21803
21804 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21805 for the initializer. If the INDEX of the elt is non-NULL, it is the
21806 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21807 as for cp_parser_initializer. */
21808
21809static vec<constructor_elt, va_gc> *
21810cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21811{
21812 vec<constructor_elt, va_gc> *v = NULL;
21813
21814 /* Assume all of the expressions are constant. */
21815 *non_constant_p = false;
21816
21817 /* Parse the rest of the list. */
21818 while (true)
21819 {
21820 cp_token *token;
21821 tree designator;
21822 tree initializer;
21823 bool clause_non_constant_p;
21824
21825 /* If the next token is an identifier and the following one is a
21826 colon, we are looking at the GNU designated-initializer
21827 syntax. */
21828 if (cp_parser_allow_gnu_extensions_p (parser)
21829 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21830 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21831 {
21832 /* Warn the user that they are using an extension. */
21833 pedwarn (input_location, OPT_Wpedantic,
21834 "ISO C++ does not allow designated initializers");
21835 /* Consume the identifier. */
21836 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21837 /* Consume the `:'. */
21838 cp_lexer_consume_token (parser->lexer);
21839 }
21840 /* Also handle the C99 syntax, '. id ='. */
21841 else if (cp_parser_allow_gnu_extensions_p (parser)
21842 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21843 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21844 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21845 {
21846 /* Warn the user that they are using an extension. */
21847 pedwarn (input_location, OPT_Wpedantic,
21848 "ISO C++ does not allow C99 designated initializers");
21849 /* Consume the `.'. */
21850 cp_lexer_consume_token (parser->lexer);
21851 /* Consume the identifier. */
21852 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21853 /* Consume the `='. */
21854 cp_lexer_consume_token (parser->lexer);
21855 }
21856 /* Also handle C99 array designators, '[ const ] ='. */
21857 else if (cp_parser_allow_gnu_extensions_p (parser)
21858 && !c_dialect_objc ()
21859 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21860 {
21861 /* In C++11, [ could start a lambda-introducer. */
21862 bool non_const = false;
21863
21864 cp_parser_parse_tentatively (parser);
21865
21866 if (!cp_parser_array_designator_p (parser))
21867 {
21868 cp_parser_simulate_error (parser);
21869 designator = NULL_TREE;
21870 }
21871 else
21872 {
21873 designator = cp_parser_constant_expression (parser, true,
21874 &non_const);
21875 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21876 cp_parser_require (parser, CPP_EQ, RT_EQ);
21877 }
21878
21879 if (!cp_parser_parse_definitely (parser))
21880 designator = NULL_TREE;
21881 else if (non_const)
21882 require_potential_rvalue_constant_expression (designator);
21883 }
21884 else
21885 designator = NULL_TREE;
21886
21887 /* Parse the initializer. */
21888 initializer = cp_parser_initializer_clause (parser,
21889 &clause_non_constant_p);
21890 /* If any clause is non-constant, so is the entire initializer. */
21891 if (clause_non_constant_p)
21892 *non_constant_p = true;
21893
21894 /* If we have an ellipsis, this is an initializer pack
21895 expansion. */
21896 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21897 {
21898 /* Consume the `...'. */
21899 cp_lexer_consume_token (parser->lexer);
21900
21901 /* Turn the initializer into an initializer expansion. */
21902 initializer = make_pack_expansion (initializer);
21903 }
21904
21905 /* Add it to the vector. */
21906 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21907
21908 /* If the next token is not a comma, we have reached the end of
21909 the list. */
21910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21911 break;
21912
21913 /* Peek at the next token. */
21914 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21915 /* If the next token is a `}', then we're still done. An
21916 initializer-clause can have a trailing `,' after the
21917 initializer-list and before the closing `}'. */
21918 if (token->type == CPP_CLOSE_BRACE)
21919 break;
21920
21921 /* Consume the `,' token. */
21922 cp_lexer_consume_token (parser->lexer);
21923 }
21924
21925 return v;
21926}
21927
21928/* Classes [gram.class] */
21929
21930/* Parse a class-name.
21931
21932 class-name:
21933 identifier
21934 template-id
21935
21936 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21937 to indicate that names looked up in dependent types should be
21938 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21939 keyword has been used to indicate that the name that appears next
21940 is a template. TAG_TYPE indicates the explicit tag given before
21941 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21942 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21943 is the class being defined in a class-head. If ENUM_OK is TRUE,
21944 enum-names are also accepted.
21945
21946 Returns the TYPE_DECL representing the class. */
21947
21948static tree
21949cp_parser_class_name (cp_parser *parser,
21950 bool typename_keyword_p,
21951 bool template_keyword_p,
21952 enum tag_types tag_type,
21953 bool check_dependency_p,
21954 bool class_head_p,
21955 bool is_declaration,
21956 bool enum_ok)
21957{
21958 tree decl;
21959 tree scope;
21960 bool typename_p;
21961 cp_token *token;
21962 tree identifier = NULL_TREE;
21963
21964 /* All class-names start with an identifier. */
21965 token = cp_lexer_peek_token (parser->lexer);
21966 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21967 {
21968 cp_parser_error (parser, "expected class-name");
21969 return error_mark_node;
21970 }
21971
21972 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21973 to a template-id, so we save it here. */
21974 scope = parser->scope;
21975 if (scope == error_mark_node)
21976 return error_mark_node;
21977
21978 /* Any name names a type if we're following the `typename' keyword
21979 in a qualified name where the enclosing scope is type-dependent. */
21980 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21981 && dependent_type_p (scope));
21982 /* Handle the common case (an identifier, but not a template-id)
21983 efficiently. */
21984 if (token->type == CPP_NAME
21985 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21986 {
21987 cp_token *identifier_token;
21988 bool ambiguous_p;
21989
21990 /* Look for the identifier. */
21991 identifier_token = cp_lexer_peek_token (parser->lexer);
21992 ambiguous_p = identifier_token->error_reported;
21993 identifier = cp_parser_identifier (parser);
21994 /* If the next token isn't an identifier, we are certainly not
21995 looking at a class-name. */
21996 if (identifier == error_mark_node)
21997 decl = error_mark_node;
21998 /* If we know this is a type-name, there's no need to look it
21999 up. */
22000 else if (typename_p)
22001 decl = identifier;
22002 else
22003 {
22004 tree ambiguous_decls;
22005 /* If we already know that this lookup is ambiguous, then
22006 we've already issued an error message; there's no reason
22007 to check again. */
22008 if (ambiguous_p)
22009 {
22010 cp_parser_simulate_error (parser);
22011 return error_mark_node;
22012 }
22013 /* If the next token is a `::', then the name must be a type
22014 name.
22015
22016 [basic.lookup.qual]
22017
22018 During the lookup for a name preceding the :: scope
22019 resolution operator, object, function, and enumerator
22020 names are ignored. */
22021 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22022 tag_type = scope_type;
22023 /* Look up the name. */
22024 decl = cp_parser_lookup_name (parser, identifier,
22025 tag_type,
22026 /*is_template=*/false,
22027 /*is_namespace=*/false,
22028 check_dependency_p,
22029 &ambiguous_decls,
22030 identifier_token->location);
22031 if (ambiguous_decls)
22032 {
22033 if (cp_parser_parsing_tentatively (parser))
22034 cp_parser_simulate_error (parser);
22035 return error_mark_node;
22036 }
22037 }
22038 }
22039 else
22040 {
22041 /* Try a template-id. */
22042 decl = cp_parser_template_id (parser, template_keyword_p,
22043 check_dependency_p,
22044 tag_type,
22045 is_declaration);
22046 if (decl == error_mark_node)
22047 return error_mark_node;
22048 }
22049
22050 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22051
22052 /* If this is a typename, create a TYPENAME_TYPE. */
22053 if (typename_p && decl != error_mark_node)
22054 {
22055 decl = make_typename_type (scope, decl, typename_type,
22056 /*complain=*/tf_error);
22057 if (decl != error_mark_node)
22058 decl = TYPE_NAME (decl);
22059 }
22060
22061 decl = strip_using_decl (decl);
22062
22063 /* Check to see that it is really the name of a class. */
22064 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22065 && identifier_p (TREE_OPERAND (decl, 0))
22066 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22067 /* Situations like this:
22068
22069 template <typename T> struct A {
22070 typename T::template X<int>::I i;
22071 };
22072
22073 are problematic. Is `T::template X<int>' a class-name? The
22074 standard does not seem to be definitive, but there is no other
22075 valid interpretation of the following `::'. Therefore, those
22076 names are considered class-names. */
22077 {
22078 decl = make_typename_type (scope, decl, tag_type, tf_error);
22079 if (decl != error_mark_node)
22080 decl = TYPE_NAME (decl);
22081 }
22082 else if (TREE_CODE (decl) != TYPE_DECL
22083 || TREE_TYPE (decl) == error_mark_node
22084 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22085 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22086 /* In Objective-C 2.0, a classname followed by '.' starts a
22087 dot-syntax expression, and it's not a type-name. */
22088 || (c_dialect_objc ()
22089 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22090 && objc_is_class_name (decl)))
22091 decl = error_mark_node;
22092
22093 if (decl == error_mark_node)
22094 cp_parser_error (parser, "expected class-name");
22095 else if (identifier && !parser->scope)
22096 maybe_note_name_used_in_class (identifier, decl);
22097
22098 return decl;
22099}
22100
22101/* Parse a class-specifier.
22102
22103 class-specifier:
22104 class-head { member-specification [opt] }
22105
22106 Returns the TREE_TYPE representing the class. */
22107
22108static tree
22109cp_parser_class_specifier_1 (cp_parser* parser)
22110{
22111 tree type;
22112 tree attributes = NULL_TREE;
22113 bool nested_name_specifier_p;
22114 unsigned saved_num_template_parameter_lists;
22115 bool saved_in_function_body;
22116 unsigned char in_statement;
22117 bool in_switch_statement_p;
22118 bool saved_in_unbraced_linkage_specification_p;
22119 tree old_scope = NULL_TREE;
22120 tree scope = NULL_TREE;
22121 cp_token *closing_brace;
22122
22123 push_deferring_access_checks (dk_no_deferred);
22124
22125 /* Parse the class-head. */
22126 type = cp_parser_class_head (parser,
22127 &nested_name_specifier_p);
22128 /* If the class-head was a semantic disaster, skip the entire body
22129 of the class. */
22130 if (!type)
22131 {
22132 cp_parser_skip_to_end_of_block_or_statement (parser);
22133 pop_deferring_access_checks ();
22134 return error_mark_node;
22135 }
22136
22137 /* Look for the `{'. */
22138 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22139 {
22140 pop_deferring_access_checks ();
22141 return error_mark_node;
22142 }
22143
22144 cp_ensure_no_omp_declare_simd (parser);
22145 cp_ensure_no_oacc_routine (parser);
22146
22147 /* Issue an error message if type-definitions are forbidden here. */
22148 cp_parser_check_type_definition (parser);
22149 /* Remember that we are defining one more class. */
22150 ++parser->num_classes_being_defined;
22151 /* Inside the class, surrounding template-parameter-lists do not
22152 apply. */
22153 saved_num_template_parameter_lists
22154 = parser->num_template_parameter_lists;
22155 parser->num_template_parameter_lists = 0;
22156 /* We are not in a function body. */
22157 saved_in_function_body = parser->in_function_body;
22158 parser->in_function_body = false;
22159 /* Or in a loop. */
22160 in_statement = parser->in_statement;
22161 parser->in_statement = 0;
22162 /* Or in a switch. */
22163 in_switch_statement_p = parser->in_switch_statement_p;
22164 parser->in_switch_statement_p = false;
22165 /* We are not immediately inside an extern "lang" block. */
22166 saved_in_unbraced_linkage_specification_p
22167 = parser->in_unbraced_linkage_specification_p;
22168 parser->in_unbraced_linkage_specification_p = false;
22169
22170 // Associate constraints with the type.
22171 if (flag_concepts)
22172 type = associate_classtype_constraints (type);
22173
22174 /* Start the class. */
22175 if (nested_name_specifier_p)
22176 {
22177 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22178 old_scope = push_inner_scope (scope);
22179 }
22180 type = begin_class_definition (type);
22181
22182 if (type == error_mark_node)
22183 /* If the type is erroneous, skip the entire body of the class. */
22184 cp_parser_skip_to_closing_brace (parser);
22185 else
22186 /* Parse the member-specification. */
22187 cp_parser_member_specification_opt (parser);
22188
22189 /* Look for the trailing `}'. */
22190 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22191 /* Look for trailing attributes to apply to this class. */
22192 if (cp_parser_allow_gnu_extensions_p (parser))
22193 attributes = cp_parser_gnu_attributes_opt (parser);
22194 if (type != error_mark_node)
22195 type = finish_struct (type, attributes);
22196 if (nested_name_specifier_p)
22197 pop_inner_scope (old_scope, scope);
22198
22199 /* We've finished a type definition. Check for the common syntax
22200 error of forgetting a semicolon after the definition. We need to
22201 be careful, as we can't just check for not-a-semicolon and be done
22202 with it; the user might have typed:
22203
22204 class X { } c = ...;
22205 class X { } *p = ...;
22206
22207 and so forth. Instead, enumerate all the possible tokens that
22208 might follow this production; if we don't see one of them, then
22209 complain and silently insert the semicolon. */
22210 {
22211 cp_token *token = cp_lexer_peek_token (parser->lexer);
22212 bool want_semicolon = true;
22213
22214 if (cp_next_tokens_can_be_std_attribute_p (parser))
22215 /* Don't try to parse c++11 attributes here. As per the
22216 grammar, that should be a task for
22217 cp_parser_decl_specifier_seq. */
22218 want_semicolon = false;
22219
22220 switch (token->type)
22221 {
22222 case CPP_NAME:
22223 case CPP_SEMICOLON:
22224 case CPP_MULT:
22225 case CPP_AND:
22226 case CPP_OPEN_PAREN:
22227 case CPP_CLOSE_PAREN:
22228 case CPP_COMMA:
22229 want_semicolon = false;
22230 break;
22231
22232 /* While it's legal for type qualifiers and storage class
22233 specifiers to follow type definitions in the grammar, only
22234 compiler testsuites contain code like that. Assume that if
22235 we see such code, then what we're really seeing is a case
22236 like:
22237
22238 class X { }
22239 const <type> var = ...;
22240
22241 or
22242
22243 class Y { }
22244 static <type> func (...) ...
22245
22246 i.e. the qualifier or specifier applies to the next
22247 declaration. To do so, however, we need to look ahead one
22248 more token to see if *that* token is a type specifier.
22249
22250 This code could be improved to handle:
22251
22252 class Z { }
22253 static const <type> var = ...; */
22254 case CPP_KEYWORD:
22255 if (keyword_is_decl_specifier (token->keyword))
22256 {
22257 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22258
22259 /* Handling user-defined types here would be nice, but very
22260 tricky. */
22261 want_semicolon
22262 = (lookahead->type == CPP_KEYWORD
22263 && keyword_begins_type_specifier (lookahead->keyword));
22264 }
22265 break;
22266 default:
22267 break;
22268 }
22269
22270 /* If we don't have a type, then something is very wrong and we
22271 shouldn't try to do anything clever. Likewise for not seeing the
22272 closing brace. */
22273 if (closing_brace && TYPE_P (type) && want_semicolon)
22274 {
22275 /* Locate the closing brace. */
22276 cp_token_position prev
22277 = cp_lexer_previous_token_position (parser->lexer);
22278 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22279 location_t loc = prev_token->location;
22280
22281 /* We want to suggest insertion of a ';' immediately *after* the
22282 closing brace, so, if we can, offset the location by 1 column. */
22283 location_t next_loc = loc;
22284 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22285 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22286
22287 rich_location richloc (line_table, next_loc);
22288
22289 /* If we successfully offset the location, suggest the fix-it. */
22290 if (next_loc != loc)
22291 richloc.add_fixit_insert_before (next_loc, ";");
22292
22293 if (CLASSTYPE_DECLARED_CLASS (type))
22294 error_at_rich_loc (&richloc,
22295 "expected %<;%> after class definition");
22296 else if (TREE_CODE (type) == RECORD_TYPE)
22297 error_at_rich_loc (&richloc,
22298 "expected %<;%> after struct definition");
22299 else if (TREE_CODE (type) == UNION_TYPE)
22300 error_at_rich_loc (&richloc,
22301 "expected %<;%> after union definition");
22302 else
22303 gcc_unreachable ();
22304
22305 /* Unget one token and smash it to look as though we encountered
22306 a semicolon in the input stream. */
22307 cp_lexer_set_token_position (parser->lexer, prev);
22308 token = cp_lexer_peek_token (parser->lexer);
22309 token->type = CPP_SEMICOLON;
22310 token->keyword = RID_MAX;
22311 }
22312 }
22313
22314 /* If this class is not itself within the scope of another class,
22315 then we need to parse the bodies of all of the queued function
22316 definitions. Note that the queued functions defined in a class
22317 are not always processed immediately following the
22318 class-specifier for that class. Consider:
22319
22320 struct A {
22321 struct B { void f() { sizeof (A); } };
22322 };
22323
22324 If `f' were processed before the processing of `A' were
22325 completed, there would be no way to compute the size of `A'.
22326 Note that the nesting we are interested in here is lexical --
22327 not the semantic nesting given by TYPE_CONTEXT. In particular,
22328 for:
22329
22330 struct A { struct B; };
22331 struct A::B { void f() { } };
22332
22333 there is no need to delay the parsing of `A::B::f'. */
22334 if (--parser->num_classes_being_defined == 0)
22335 {
22336 tree decl;
22337 tree class_type = NULL_TREE;
22338 tree pushed_scope = NULL_TREE;
22339 unsigned ix;
22340 cp_default_arg_entry *e;
22341 tree save_ccp, save_ccr;
22342
22343 /* In a first pass, parse default arguments to the functions.
22344 Then, in a second pass, parse the bodies of the functions.
22345 This two-phased approach handles cases like:
22346
22347 struct S {
22348 void f() { g(); }
22349 void g(int i = 3);
22350 };
22351
22352 */
22353 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22354 {
22355 decl = e->decl;
22356 /* If there are default arguments that have not yet been processed,
22357 take care of them now. */
22358 if (class_type != e->class_type)
22359 {
22360 if (pushed_scope)
22361 pop_scope (pushed_scope);
22362 class_type = e->class_type;
22363 pushed_scope = push_scope (class_type);
22364 }
22365 /* Make sure that any template parameters are in scope. */
22366 maybe_begin_member_template_processing (decl);
22367 /* Parse the default argument expressions. */
22368 cp_parser_late_parsing_default_args (parser, decl);
22369 /* Remove any template parameters from the symbol table. */
22370 maybe_end_member_template_processing ();
22371 }
22372 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22373 /* Now parse any NSDMIs. */
22374 save_ccp = current_class_ptr;
22375 save_ccr = current_class_ref;
22376 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22377 {
22378 if (class_type != DECL_CONTEXT (decl))
22379 {
22380 if (pushed_scope)
22381 pop_scope (pushed_scope);
22382 class_type = DECL_CONTEXT (decl);
22383 pushed_scope = push_scope (class_type);
22384 }
22385 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22386 cp_parser_late_parsing_nsdmi (parser, decl);
22387 }
22388 vec_safe_truncate (unparsed_nsdmis, 0);
22389 current_class_ptr = save_ccp;
22390 current_class_ref = save_ccr;
22391 if (pushed_scope)
22392 pop_scope (pushed_scope);
22393
22394 /* Now do some post-NSDMI bookkeeping. */
22395 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22396 after_nsdmi_defaulted_late_checks (class_type);
22397 vec_safe_truncate (unparsed_classes, 0);
22398 after_nsdmi_defaulted_late_checks (type);
22399
22400 /* Now parse the body of the functions. */
22401 if (flag_openmp)
22402 {
22403 /* OpenMP UDRs need to be parsed before all other functions. */
22404 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22405 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22406 cp_parser_late_parsing_for_member (parser, decl);
22407 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22408 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22409 cp_parser_late_parsing_for_member (parser, decl);
22410 }
22411 else
22412 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22413 cp_parser_late_parsing_for_member (parser, decl);
22414 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22415 }
22416 else
22417 vec_safe_push (unparsed_classes, type);
22418
22419 /* Put back any saved access checks. */
22420 pop_deferring_access_checks ();
22421
22422 /* Restore saved state. */
22423 parser->in_switch_statement_p = in_switch_statement_p;
22424 parser->in_statement = in_statement;
22425 parser->in_function_body = saved_in_function_body;
22426 parser->num_template_parameter_lists
22427 = saved_num_template_parameter_lists;
22428 parser->in_unbraced_linkage_specification_p
22429 = saved_in_unbraced_linkage_specification_p;
22430
22431 return type;
22432}
22433
22434static tree
22435cp_parser_class_specifier (cp_parser* parser)
22436{
22437 tree ret;
22438 timevar_push (TV_PARSE_STRUCT);
22439 ret = cp_parser_class_specifier_1 (parser);
22440 timevar_pop (TV_PARSE_STRUCT);
22441 return ret;
22442}
22443
22444/* Parse a class-head.
22445
22446 class-head:
22447 class-key identifier [opt] base-clause [opt]
22448 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22449 class-key nested-name-specifier [opt] template-id
22450 base-clause [opt]
22451
22452 class-virt-specifier:
22453 final
22454
22455 GNU Extensions:
22456 class-key attributes identifier [opt] base-clause [opt]
22457 class-key attributes nested-name-specifier identifier base-clause [opt]
22458 class-key attributes nested-name-specifier [opt] template-id
22459 base-clause [opt]
22460
22461 Upon return BASES is initialized to the list of base classes (or
22462 NULL, if there are none) in the same form returned by
22463 cp_parser_base_clause.
22464
22465 Returns the TYPE of the indicated class. Sets
22466 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22467 involving a nested-name-specifier was used, and FALSE otherwise.
22468
22469 Returns error_mark_node if this is not a class-head.
22470
22471 Returns NULL_TREE if the class-head is syntactically valid, but
22472 semantically invalid in a way that means we should skip the entire
22473 body of the class. */
22474
22475static tree
22476cp_parser_class_head (cp_parser* parser,
22477 bool* nested_name_specifier_p)
22478{
22479 tree nested_name_specifier;
22480 enum tag_types class_key;
22481 tree id = NULL_TREE;
22482 tree type = NULL_TREE;
22483 tree attributes;
22484 tree bases;
22485 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22486 bool template_id_p = false;
22487 bool qualified_p = false;
22488 bool invalid_nested_name_p = false;
22489 bool invalid_explicit_specialization_p = false;
22490 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22491 tree pushed_scope = NULL_TREE;
22492 unsigned num_templates;
22493 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22494 /* Assume no nested-name-specifier will be present. */
22495 *nested_name_specifier_p = false;
22496 /* Assume no template parameter lists will be used in defining the
22497 type. */
22498 num_templates = 0;
22499 parser->colon_corrects_to_scope_p = false;
22500
22501 /* Look for the class-key. */
22502 class_key = cp_parser_class_key (parser);
22503 if (class_key == none_type)
22504 return error_mark_node;
22505
22506 location_t class_head_start_location = input_location;
22507
22508 /* Parse the attributes. */
22509 attributes = cp_parser_attributes_opt (parser);
22510
22511 /* If the next token is `::', that is invalid -- but sometimes
22512 people do try to write:
22513
22514 struct ::S {};
22515
22516 Handle this gracefully by accepting the extra qualifier, and then
22517 issuing an error about it later if this really is a
22518 class-head. If it turns out just to be an elaborated type
22519 specifier, remain silent. */
22520 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22521 qualified_p = true;
22522
22523 push_deferring_access_checks (dk_no_check);
22524
22525 /* Determine the name of the class. Begin by looking for an
22526 optional nested-name-specifier. */
22527 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22528 nested_name_specifier
22529 = cp_parser_nested_name_specifier_opt (parser,
22530 /*typename_keyword_p=*/false,
22531 /*check_dependency_p=*/false,
22532 /*type_p=*/true,
22533 /*is_declaration=*/false);
22534 /* If there was a nested-name-specifier, then there *must* be an
22535 identifier. */
22536 if (nested_name_specifier)
22537 {
22538 type_start_token = cp_lexer_peek_token (parser->lexer);
22539 /* Although the grammar says `identifier', it really means
22540 `class-name' or `template-name'. You are only allowed to
22541 define a class that has already been declared with this
22542 syntax.
22543
22544 The proposed resolution for Core Issue 180 says that wherever
22545 you see `class T::X' you should treat `X' as a type-name.
22546
22547 It is OK to define an inaccessible class; for example:
22548
22549 class A { class B; };
22550 class A::B {};
22551
22552 We do not know if we will see a class-name, or a
22553 template-name. We look for a class-name first, in case the
22554 class-name is a template-id; if we looked for the
22555 template-name first we would stop after the template-name. */
22556 cp_parser_parse_tentatively (parser);
22557 type = cp_parser_class_name (parser,
22558 /*typename_keyword_p=*/false,
22559 /*template_keyword_p=*/false,
22560 class_type,
22561 /*check_dependency_p=*/false,
22562 /*class_head_p=*/true,
22563 /*is_declaration=*/false);
22564 /* If that didn't work, ignore the nested-name-specifier. */
22565 if (!cp_parser_parse_definitely (parser))
22566 {
22567 invalid_nested_name_p = true;
22568 type_start_token = cp_lexer_peek_token (parser->lexer);
22569 id = cp_parser_identifier (parser);
22570 if (id == error_mark_node)
22571 id = NULL_TREE;
22572 }
22573 /* If we could not find a corresponding TYPE, treat this
22574 declaration like an unqualified declaration. */
22575 if (type == error_mark_node)
22576 nested_name_specifier = NULL_TREE;
22577 /* Otherwise, count the number of templates used in TYPE and its
22578 containing scopes. */
22579 else
22580 {
22581 tree scope;
22582
22583 for (scope = TREE_TYPE (type);
22584 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22585 scope = get_containing_scope (scope))
22586 if (TYPE_P (scope)
22587 && CLASS_TYPE_P (scope)
22588 && CLASSTYPE_TEMPLATE_INFO (scope)
22589 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22590 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22591 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22592 ++num_templates;
22593 }
22594 }
22595 /* Otherwise, the identifier is optional. */
22596 else
22597 {
22598 /* We don't know whether what comes next is a template-id,
22599 an identifier, or nothing at all. */
22600 cp_parser_parse_tentatively (parser);
22601 /* Check for a template-id. */
22602 type_start_token = cp_lexer_peek_token (parser->lexer);
22603 id = cp_parser_template_id (parser,
22604 /*template_keyword_p=*/false,
22605 /*check_dependency_p=*/true,
22606 class_key,
22607 /*is_declaration=*/true);
22608 /* If that didn't work, it could still be an identifier. */
22609 if (!cp_parser_parse_definitely (parser))
22610 {
22611 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22612 {
22613 type_start_token = cp_lexer_peek_token (parser->lexer);
22614 id = cp_parser_identifier (parser);
22615 }
22616 else
22617 id = NULL_TREE;
22618 }
22619 else
22620 {
22621 template_id_p = true;
22622 ++num_templates;
22623 }
22624 }
22625
22626 pop_deferring_access_checks ();
22627
22628 if (id)
22629 {
22630 cp_parser_check_for_invalid_template_id (parser, id,
22631 class_key,
22632 type_start_token->location);
22633 }
22634 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22635
22636 /* If it's not a `:' or a `{' then we can't really be looking at a
22637 class-head, since a class-head only appears as part of a
22638 class-specifier. We have to detect this situation before calling
22639 xref_tag, since that has irreversible side-effects. */
22640 if (!cp_parser_next_token_starts_class_definition_p (parser))
22641 {
22642 cp_parser_error (parser, "expected %<{%> or %<:%>");
22643 type = error_mark_node;
22644 goto out;
22645 }
22646
22647 /* At this point, we're going ahead with the class-specifier, even
22648 if some other problem occurs. */
22649 cp_parser_commit_to_tentative_parse (parser);
22650 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22651 {
22652 cp_parser_error (parser,
22653 "cannot specify %<override%> for a class");
22654 type = error_mark_node;
22655 goto out;
22656 }
22657 /* Issue the error about the overly-qualified name now. */
22658 if (qualified_p)
22659 {
22660 cp_parser_error (parser,
22661 "global qualification of class name is invalid");
22662 type = error_mark_node;
22663 goto out;
22664 }
22665 else if (invalid_nested_name_p)
22666 {
22667 cp_parser_error (parser,
22668 "qualified name does not name a class");
22669 type = error_mark_node;
22670 goto out;
22671 }
22672 else if (nested_name_specifier)
22673 {
22674 tree scope;
22675
22676 /* Reject typedef-names in class heads. */
22677 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22678 {
22679 error_at (type_start_token->location,
22680 "invalid class name in declaration of %qD",
22681 type);
22682 type = NULL_TREE;
22683 goto done;
22684 }
22685
22686 /* Figure out in what scope the declaration is being placed. */
22687 scope = current_scope ();
22688 /* If that scope does not contain the scope in which the
22689 class was originally declared, the program is invalid. */
22690 if (scope && !is_ancestor (scope, nested_name_specifier))
22691 {
22692 if (at_namespace_scope_p ())
22693 error_at (type_start_token->location,
22694 "declaration of %qD in namespace %qD which does not "
22695 "enclose %qD",
22696 type, scope, nested_name_specifier);
22697 else
22698 error_at (type_start_token->location,
22699 "declaration of %qD in %qD which does not enclose %qD",
22700 type, scope, nested_name_specifier);
22701 type = NULL_TREE;
22702 goto done;
22703 }
22704 /* [dcl.meaning]
22705
22706 A declarator-id shall not be qualified except for the
22707 definition of a ... nested class outside of its class
22708 ... [or] the definition or explicit instantiation of a
22709 class member of a namespace outside of its namespace. */
22710 if (scope == nested_name_specifier)
22711 {
22712 permerror (nested_name_specifier_token_start->location,
22713 "extra qualification not allowed");
22714 nested_name_specifier = NULL_TREE;
22715 num_templates = 0;
22716 }
22717 }
22718 /* An explicit-specialization must be preceded by "template <>". If
22719 it is not, try to recover gracefully. */
22720 if (at_namespace_scope_p ()
22721 && parser->num_template_parameter_lists == 0
22722 && !processing_template_parmlist
22723 && template_id_p)
22724 {
22725 /* Build a location of this form:
22726 struct typename <ARGS>
22727 ^~~~~~~~~~~~~~~~~~~~~~
22728 with caret==start at the start token, and
22729 finishing at the end of the type. */
22730 location_t reported_loc
22731 = make_location (class_head_start_location,
22732 class_head_start_location,
22733 get_finish (type_start_token->location));
22734 rich_location richloc (line_table, reported_loc);
22735 richloc.add_fixit_insert_before (class_head_start_location,
22736 "template <> ");
22737 error_at_rich_loc
22738 (&richloc,
22739 "an explicit specialization must be preceded by %<template <>%>");
22740 invalid_explicit_specialization_p = true;
22741 /* Take the same action that would have been taken by
22742 cp_parser_explicit_specialization. */
22743 ++parser->num_template_parameter_lists;
22744 begin_specialization ();
22745 }
22746 /* There must be no "return" statements between this point and the
22747 end of this function; set "type "to the correct return value and
22748 use "goto done;" to return. */
22749 /* Make sure that the right number of template parameters were
22750 present. */
22751 if (!cp_parser_check_template_parameters (parser, num_templates,
22752 type_start_token->location,
22753 /*declarator=*/NULL))
22754 {
22755 /* If something went wrong, there is no point in even trying to
22756 process the class-definition. */
22757 type = NULL_TREE;
22758 goto done;
22759 }
22760
22761 /* Look up the type. */
22762 if (template_id_p)
22763 {
22764 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22765 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22766 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22767 {
22768 error_at (type_start_token->location,
22769 "function template %qD redeclared as a class template", id);
22770 type = error_mark_node;
22771 }
22772 else
22773 {
22774 type = TREE_TYPE (id);
22775 type = maybe_process_partial_specialization (type);
22776
22777 /* Check the scope while we still know whether or not we had a
22778 nested-name-specifier. */
22779 if (type != error_mark_node)
22780 check_unqualified_spec_or_inst (type, type_start_token->location);
22781 }
22782 if (nested_name_specifier)
22783 pushed_scope = push_scope (nested_name_specifier);
22784 }
22785 else if (nested_name_specifier)
22786 {
22787 tree class_type;
22788
22789 /* Given:
22790
22791 template <typename T> struct S { struct T };
22792 template <typename T> struct S<T>::T { };
22793
22794 we will get a TYPENAME_TYPE when processing the definition of
22795 `S::T'. We need to resolve it to the actual type before we
22796 try to define it. */
22797 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22798 {
22799 class_type = resolve_typename_type (TREE_TYPE (type),
22800 /*only_current_p=*/false);
22801 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22802 type = TYPE_NAME (class_type);
22803 else
22804 {
22805 cp_parser_error (parser, "could not resolve typename type");
22806 type = error_mark_node;
22807 }
22808 }
22809
22810 if (maybe_process_partial_specialization (TREE_TYPE (type))
22811 == error_mark_node)
22812 {
22813 type = NULL_TREE;
22814 goto done;
22815 }
22816
22817 class_type = current_class_type;
22818 /* Enter the scope indicated by the nested-name-specifier. */
22819 pushed_scope = push_scope (nested_name_specifier);
22820 /* Get the canonical version of this type. */
22821 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22822 /* Call push_template_decl if it seems like we should be defining a
22823 template either from the template headers or the type we're
22824 defining, so that we diagnose both extra and missing headers. */
22825 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22826 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22827 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22828 {
22829 type = push_template_decl (type);
22830 if (type == error_mark_node)
22831 {
22832 type = NULL_TREE;
22833 goto done;
22834 }
22835 }
22836
22837 type = TREE_TYPE (type);
22838 *nested_name_specifier_p = true;
22839 }
22840 else /* The name is not a nested name. */
22841 {
22842 /* If the class was unnamed, create a dummy name. */
22843 if (!id)
22844 id = make_anon_name ();
22845 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22846 ? ts_within_enclosing_non_class
22847 : ts_current);
22848 type = xref_tag (class_key, id, tag_scope,
22849 parser->num_template_parameter_lists);
22850 }
22851
22852 /* Indicate whether this class was declared as a `class' or as a
22853 `struct'. */
22854 if (TREE_CODE (type) == RECORD_TYPE)
22855 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22856 cp_parser_check_class_key (class_key, type);
22857
22858 /* If this type was already complete, and we see another definition,
22859 that's an error. */
22860 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22861 {
22862 error_at (type_start_token->location, "redefinition of %q#T",
22863 type);
22864 inform (location_of (type), "previous definition of %q#T",
22865 type);
22866 type = NULL_TREE;
22867 goto done;
22868 }
22869 else if (type == error_mark_node)
22870 type = NULL_TREE;
22871
22872 if (type)
22873 {
22874 /* Apply attributes now, before any use of the class as a template
22875 argument in its base list. */
22876 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22877 fixup_attribute_variants (type);
22878 }
22879
22880 /* We will have entered the scope containing the class; the names of
22881 base classes should be looked up in that context. For example:
22882
22883 struct A { struct B {}; struct C; };
22884 struct A::C : B {};
22885
22886 is valid. */
22887
22888 /* Get the list of base-classes, if there is one. */
22889 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22890 {
22891 /* PR59482: enter the class scope so that base-specifiers are looked
22892 up correctly. */
22893 if (type)
22894 pushclass (type);
22895 bases = cp_parser_base_clause (parser);
22896 /* PR59482: get out of the previously pushed class scope so that the
22897 subsequent pops pop the right thing. */
22898 if (type)
22899 popclass ();
22900 }
22901 else
22902 bases = NULL_TREE;
22903
22904 /* If we're really defining a class, process the base classes.
22905 If they're invalid, fail. */
22906 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22907 xref_basetypes (type, bases);
22908
22909 done:
22910 /* Leave the scope given by the nested-name-specifier. We will
22911 enter the class scope itself while processing the members. */
22912 if (pushed_scope)
22913 pop_scope (pushed_scope);
22914
22915 if (invalid_explicit_specialization_p)
22916 {
22917 end_specialization ();
22918 --parser->num_template_parameter_lists;
22919 }
22920
22921 if (type)
22922 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22923 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22924 CLASSTYPE_FINAL (type) = 1;
22925 out:
22926 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22927 return type;
22928}
22929
22930/* Parse a class-key.
22931
22932 class-key:
22933 class
22934 struct
22935 union
22936
22937 Returns the kind of class-key specified, or none_type to indicate
22938 error. */
22939
22940static enum tag_types
22941cp_parser_class_key (cp_parser* parser)
22942{
22943 cp_token *token;
22944 enum tag_types tag_type;
22945
22946 /* Look for the class-key. */
22947 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22948 if (!token)
22949 return none_type;
22950
22951 /* Check to see if the TOKEN is a class-key. */
22952 tag_type = cp_parser_token_is_class_key (token);
22953 if (!tag_type)
22954 cp_parser_error (parser, "expected class-key");
22955 return tag_type;
22956}
22957
22958/* Parse a type-parameter-key.
22959
22960 type-parameter-key:
22961 class
22962 typename
22963 */
22964
22965static void
22966cp_parser_type_parameter_key (cp_parser* parser)
22967{
22968 /* Look for the type-parameter-key. */
22969 enum tag_types tag_type = none_type;
22970 cp_token *token = cp_lexer_peek_token (parser->lexer);
22971 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22972 {
22973 cp_lexer_consume_token (parser->lexer);
22974 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22975 /* typename is not allowed in a template template parameter
22976 by the standard until C++1Z. */
22977 pedwarn (token->location, OPT_Wpedantic,
22978 "ISO C++ forbids typename key in template template parameter;"
22979 " use -std=c++1z or -std=gnu++1z");
22980 }
22981 else
22982 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22983
22984 return;
22985}
22986
22987/* Parse an (optional) member-specification.
22988
22989 member-specification:
22990 member-declaration member-specification [opt]
22991 access-specifier : member-specification [opt] */
22992
22993static void
22994cp_parser_member_specification_opt (cp_parser* parser)
22995{
22996 while (true)
22997 {
22998 cp_token *token;
22999 enum rid keyword;
23000
23001 /* Peek at the next token. */
23002 token = cp_lexer_peek_token (parser->lexer);
23003 /* If it's a `}', or EOF then we've seen all the members. */
23004 if (token->type == CPP_CLOSE_BRACE
23005 || token->type == CPP_EOF
23006 || token->type == CPP_PRAGMA_EOL)
23007 break;
23008
23009 /* See if this token is a keyword. */
23010 keyword = token->keyword;
23011 switch (keyword)
23012 {
23013 case RID_PUBLIC:
23014 case RID_PROTECTED:
23015 case RID_PRIVATE:
23016 /* Consume the access-specifier. */
23017 cp_lexer_consume_token (parser->lexer);
23018 /* Remember which access-specifier is active. */
23019 current_access_specifier = token->u.value;
23020 /* Look for the `:'. */
23021 cp_parser_require (parser, CPP_COLON, RT_COLON);
23022 break;
23023
23024 default:
23025 /* Accept #pragmas at class scope. */
23026 if (token->type == CPP_PRAGMA)
23027 {
23028 cp_parser_pragma (parser, pragma_member, NULL);
23029 break;
23030 }
23031
23032 /* Otherwise, the next construction must be a
23033 member-declaration. */
23034 cp_parser_member_declaration (parser);
23035 }
23036 }
23037}
23038
23039/* Parse a member-declaration.
23040
23041 member-declaration:
23042 decl-specifier-seq [opt] member-declarator-list [opt] ;
23043 function-definition ; [opt]
23044 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23045 using-declaration
23046 template-declaration
23047 alias-declaration
23048
23049 member-declarator-list:
23050 member-declarator
23051 member-declarator-list , member-declarator
23052
23053 member-declarator:
23054 declarator pure-specifier [opt]
23055 declarator constant-initializer [opt]
23056 identifier [opt] : constant-expression
23057
23058 GNU Extensions:
23059
23060 member-declaration:
23061 __extension__ member-declaration
23062
23063 member-declarator:
23064 declarator attributes [opt] pure-specifier [opt]
23065 declarator attributes [opt] constant-initializer [opt]
23066 identifier [opt] attributes [opt] : constant-expression
23067
23068 C++0x Extensions:
23069
23070 member-declaration:
23071 static_assert-declaration */
23072
23073static void
23074cp_parser_member_declaration (cp_parser* parser)
23075{
23076 cp_decl_specifier_seq decl_specifiers;
23077 tree prefix_attributes;
23078 tree decl;
23079 int declares_class_or_enum;
23080 bool friend_p;
23081 cp_token *token = NULL;
23082 cp_token *decl_spec_token_start = NULL;
23083 cp_token *initializer_token_start = NULL;
23084 int saved_pedantic;
23085 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23086
23087 /* Check for the `__extension__' keyword. */
23088 if (cp_parser_extension_opt (parser, &saved_pedantic))
23089 {
23090 /* Recurse. */
23091 cp_parser_member_declaration (parser);
23092 /* Restore the old value of the PEDANTIC flag. */
23093 pedantic = saved_pedantic;
23094
23095 return;
23096 }
23097
23098 /* Check for a template-declaration. */
23099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23100 {
23101 /* An explicit specialization here is an error condition, and we
23102 expect the specialization handler to detect and report this. */
23103 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23104 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23105 cp_parser_explicit_specialization (parser);
23106 else
23107 cp_parser_template_declaration (parser, /*member_p=*/true);
23108
23109 return;
23110 }
23111 /* Check for a template introduction. */
23112 else if (cp_parser_template_declaration_after_export (parser, true))
23113 return;
23114
23115 /* Check for a using-declaration. */
23116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23117 {
23118 if (cxx_dialect < cxx11)
23119 {
23120 /* Parse the using-declaration. */
23121 cp_parser_using_declaration (parser,
23122 /*access_declaration_p=*/false);
23123 return;
23124 }
23125 else
23126 {
23127 tree decl;
23128 bool alias_decl_expected;
23129 cp_parser_parse_tentatively (parser);
23130 decl = cp_parser_alias_declaration (parser);
23131 /* Note that if we actually see the '=' token after the
23132 identifier, cp_parser_alias_declaration commits the
23133 tentative parse. In that case, we really expect an
23134 alias-declaration. Otherwise, we expect a using
23135 declaration. */
23136 alias_decl_expected =
23137 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23138 cp_parser_parse_definitely (parser);
23139
23140 if (alias_decl_expected)
23141 finish_member_declaration (decl);
23142 else
23143 cp_parser_using_declaration (parser,
23144 /*access_declaration_p=*/false);
23145 return;
23146 }
23147 }
23148
23149 /* Check for @defs. */
23150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23151 {
23152 tree ivar, member;
23153 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23154 ivar = ivar_chains;
23155 while (ivar)
23156 {
23157 member = ivar;
23158 ivar = TREE_CHAIN (member);
23159 TREE_CHAIN (member) = NULL_TREE;
23160 finish_member_declaration (member);
23161 }
23162 return;
23163 }
23164
23165 /* If the next token is `static_assert' we have a static assertion. */
23166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23167 {
23168 cp_parser_static_assert (parser, /*member_p=*/true);
23169 return;
23170 }
23171
23172 parser->colon_corrects_to_scope_p = false;
23173
23174 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23175 goto out;
23176
23177 /* Parse the decl-specifier-seq. */
23178 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23179 cp_parser_decl_specifier_seq (parser,
23180 CP_PARSER_FLAGS_OPTIONAL,
23181 &decl_specifiers,
23182 &declares_class_or_enum);
23183 /* Check for an invalid type-name. */
23184 if (!decl_specifiers.any_type_specifiers_p
23185 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23186 goto out;
23187 /* If there is no declarator, then the decl-specifier-seq should
23188 specify a type. */
23189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23190 {
23191 /* If there was no decl-specifier-seq, and the next token is a
23192 `;', then we have something like:
23193
23194 struct S { ; };
23195
23196 [class.mem]
23197
23198 Each member-declaration shall declare at least one member
23199 name of the class. */
23200 if (!decl_specifiers.any_specifiers_p)
23201 {
23202 cp_token *token = cp_lexer_peek_token (parser->lexer);
23203 if (!in_system_header_at (token->location))
23204 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23205 }
23206 else
23207 {
23208 tree type;
23209
23210 /* See if this declaration is a friend. */
23211 friend_p = cp_parser_friend_p (&decl_specifiers);
23212 /* If there were decl-specifiers, check to see if there was
23213 a class-declaration. */
23214 type = check_tag_decl (&decl_specifiers,
23215 /*explicit_type_instantiation_p=*/false);
23216 /* Nested classes have already been added to the class, but
23217 a `friend' needs to be explicitly registered. */
23218 if (friend_p)
23219 {
23220 /* If the `friend' keyword was present, the friend must
23221 be introduced with a class-key. */
23222 if (!declares_class_or_enum && cxx_dialect < cxx11)
23223 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23224 "in C++03 a class-key must be used "
23225 "when declaring a friend");
23226 /* In this case:
23227
23228 template <typename T> struct A {
23229 friend struct A<T>::B;
23230 };
23231
23232 A<T>::B will be represented by a TYPENAME_TYPE, and
23233 therefore not recognized by check_tag_decl. */
23234 if (!type)
23235 {
23236 type = decl_specifiers.type;
23237 if (type && TREE_CODE (type) == TYPE_DECL)
23238 type = TREE_TYPE (type);
23239 }
23240 if (!type || !TYPE_P (type))
23241 error_at (decl_spec_token_start->location,
23242 "friend declaration does not name a class or "
23243 "function");
23244 else
23245 make_friend_class (current_class_type, type,
23246 /*complain=*/true);
23247 }
23248 /* If there is no TYPE, an error message will already have
23249 been issued. */
23250 else if (!type || type == error_mark_node)
23251 ;
23252 /* An anonymous aggregate has to be handled specially; such
23253 a declaration really declares a data member (with a
23254 particular type), as opposed to a nested class. */
23255 else if (ANON_AGGR_TYPE_P (type))
23256 {
23257 /* C++11 9.5/6. */
23258 if (decl_specifiers.storage_class != sc_none)
23259 error_at (decl_spec_token_start->location,
23260 "a storage class on an anonymous aggregate "
23261 "in class scope is not allowed");
23262
23263 /* Remove constructors and such from TYPE, now that we
23264 know it is an anonymous aggregate. */
23265 fixup_anonymous_aggr (type);
23266 /* And make the corresponding data member. */
23267 decl = build_decl (decl_spec_token_start->location,
23268 FIELD_DECL, NULL_TREE, type);
23269 /* Add it to the class. */
23270 finish_member_declaration (decl);
23271 }
23272 else
23273 cp_parser_check_access_in_redeclaration
23274 (TYPE_NAME (type),
23275 decl_spec_token_start->location);
23276 }
23277 }
23278 else
23279 {
23280 bool assume_semicolon = false;
23281
23282 /* Clear attributes from the decl_specifiers but keep them
23283 around as prefix attributes that apply them to the entity
23284 being declared. */
23285 prefix_attributes = decl_specifiers.attributes;
23286 decl_specifiers.attributes = NULL_TREE;
23287
23288 /* See if these declarations will be friends. */
23289 friend_p = cp_parser_friend_p (&decl_specifiers);
23290
23291 /* Keep going until we hit the `;' at the end of the
23292 declaration. */
23293 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23294 {
23295 tree attributes = NULL_TREE;
23296 tree first_attribute;
23297
23298 /* Peek at the next token. */
23299 token = cp_lexer_peek_token (parser->lexer);
23300
23301 /* Check for a bitfield declaration. */
23302 if (token->type == CPP_COLON
23303 || (token->type == CPP_NAME
23304 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23305 == CPP_COLON))
23306 {
23307 tree identifier;
23308 tree width;
23309
23310 /* Get the name of the bitfield. Note that we cannot just
23311 check TOKEN here because it may have been invalidated by
23312 the call to cp_lexer_peek_nth_token above. */
23313 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23314 identifier = cp_parser_identifier (parser);
23315 else
23316 identifier = NULL_TREE;
23317
23318 /* Consume the `:' token. */
23319 cp_lexer_consume_token (parser->lexer);
23320 /* Get the width of the bitfield. */
23321 width
23322 = cp_parser_constant_expression (parser);
23323
23324 /* Look for attributes that apply to the bitfield. */
23325 attributes = cp_parser_attributes_opt (parser);
23326 /* Remember which attributes are prefix attributes and
23327 which are not. */
23328 first_attribute = attributes;
23329 /* Combine the attributes. */
23330 attributes = attr_chainon (prefix_attributes, attributes);
23331
23332 /* Create the bitfield declaration. */
23333 decl = grokbitfield (identifier
23334 ? make_id_declarator (NULL_TREE,
23335 identifier,
23336 sfk_none)
23337 : NULL,
23338 &decl_specifiers,
23339 width,
23340 attributes);
23341 }
23342 else
23343 {
23344 cp_declarator *declarator;
23345 tree initializer;
23346 tree asm_specification;
23347 int ctor_dtor_or_conv_p;
23348
23349 /* Parse the declarator. */
23350 declarator
23351 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23352 &ctor_dtor_or_conv_p,
23353 /*parenthesized_p=*/NULL,
23354 /*member_p=*/true,
23355 friend_p);
23356
23357 /* If something went wrong parsing the declarator, make sure
23358 that we at least consume some tokens. */
23359 if (declarator == cp_error_declarator)
23360 {
23361 /* Skip to the end of the statement. */
23362 cp_parser_skip_to_end_of_statement (parser);
23363 /* If the next token is not a semicolon, that is
23364 probably because we just skipped over the body of
23365 a function. So, we consume a semicolon if
23366 present, but do not issue an error message if it
23367 is not present. */
23368 if (cp_lexer_next_token_is (parser->lexer,
23369 CPP_SEMICOLON))
23370 cp_lexer_consume_token (parser->lexer);
23371 goto out;
23372 }
23373
23374 if (declares_class_or_enum & 2)
23375 cp_parser_check_for_definition_in_return_type
23376 (declarator, decl_specifiers.type,
23377 decl_specifiers.locations[ds_type_spec]);
23378
23379 /* Look for an asm-specification. */
23380 asm_specification = cp_parser_asm_specification_opt (parser);
23381 /* Look for attributes that apply to the declaration. */
23382 attributes = cp_parser_attributes_opt (parser);
23383 /* Remember which attributes are prefix attributes and
23384 which are not. */
23385 first_attribute = attributes;
23386 /* Combine the attributes. */
23387 attributes = attr_chainon (prefix_attributes, attributes);
23388
23389 /* If it's an `=', then we have a constant-initializer or a
23390 pure-specifier. It is not correct to parse the
23391 initializer before registering the member declaration
23392 since the member declaration should be in scope while
23393 its initializer is processed. However, the rest of the
23394 front end does not yet provide an interface that allows
23395 us to handle this correctly. */
23396 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23397 {
23398 /* In [class.mem]:
23399
23400 A pure-specifier shall be used only in the declaration of
23401 a virtual function.
23402
23403 A member-declarator can contain a constant-initializer
23404 only if it declares a static member of integral or
23405 enumeration type.
23406
23407 Therefore, if the DECLARATOR is for a function, we look
23408 for a pure-specifier; otherwise, we look for a
23409 constant-initializer. When we call `grokfield', it will
23410 perform more stringent semantics checks. */
23411 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23412 if (function_declarator_p (declarator)
23413 || (decl_specifiers.type
23414 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23415 && declarator->kind == cdk_id
23416 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23417 == FUNCTION_TYPE)))
23418 initializer = cp_parser_pure_specifier (parser);
23419 else if (decl_specifiers.storage_class != sc_static)
23420 initializer = cp_parser_save_nsdmi (parser);
23421 else if (cxx_dialect >= cxx11)
23422 {
23423 bool nonconst;
23424 /* Don't require a constant rvalue in C++11, since we
23425 might want a reference constant. We'll enforce
23426 constancy later. */
23427 cp_lexer_consume_token (parser->lexer);
23428 /* Parse the initializer. */
23429 initializer = cp_parser_initializer_clause (parser,
23430 &nonconst);
23431 }
23432 else
23433 /* Parse the initializer. */
23434 initializer = cp_parser_constant_initializer (parser);
23435 }
23436 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23437 && !function_declarator_p (declarator))
23438 {
23439 bool x;
23440 if (decl_specifiers.storage_class != sc_static)
23441 initializer = cp_parser_save_nsdmi (parser);
23442 else
23443 initializer = cp_parser_initializer (parser, &x, &x);
23444 }
23445 /* Otherwise, there is no initializer. */
23446 else
23447 initializer = NULL_TREE;
23448
23449 /* See if we are probably looking at a function
23450 definition. We are certainly not looking at a
23451 member-declarator. Calling `grokfield' has
23452 side-effects, so we must not do it unless we are sure
23453 that we are looking at a member-declarator. */
23454 if (cp_parser_token_starts_function_definition_p
23455 (cp_lexer_peek_token (parser->lexer)))
23456 {
23457 /* The grammar does not allow a pure-specifier to be
23458 used when a member function is defined. (It is
23459 possible that this fact is an oversight in the
23460 standard, since a pure function may be defined
23461 outside of the class-specifier. */
23462 if (initializer && initializer_token_start)
23463 error_at (initializer_token_start->location,
23464 "pure-specifier on function-definition");
23465 decl = cp_parser_save_member_function_body (parser,
23466 &decl_specifiers,
23467 declarator,
23468 attributes);
23469 if (parser->fully_implicit_function_template_p)
23470 decl = finish_fully_implicit_template (parser, decl);
23471 /* If the member was not a friend, declare it here. */
23472 if (!friend_p)
23473 finish_member_declaration (decl);
23474 /* Peek at the next token. */
23475 token = cp_lexer_peek_token (parser->lexer);
23476 /* If the next token is a semicolon, consume it. */
23477 if (token->type == CPP_SEMICOLON)
23478 cp_lexer_consume_token (parser->lexer);
23479 goto out;
23480 }
23481 else
23482 if (declarator->kind == cdk_function)
23483 declarator->id_loc = token->location;
23484 /* Create the declaration. */
23485 decl = grokfield (declarator, &decl_specifiers,
23486 initializer, /*init_const_expr_p=*/true,
23487 asm_specification, attributes);
23488 if (parser->fully_implicit_function_template_p)
23489 {
23490 if (friend_p)
23491 finish_fully_implicit_template (parser, 0);
23492 else
23493 decl = finish_fully_implicit_template (parser, decl);
23494 }
23495 }
23496
23497 cp_finalize_omp_declare_simd (parser, decl);
23498 cp_finalize_oacc_routine (parser, decl, false);
23499
23500 /* Reset PREFIX_ATTRIBUTES. */
23501 if (attributes != error_mark_node)
23502 {
23503 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23504 attributes = TREE_CHAIN (attributes);
23505 if (attributes)
23506 TREE_CHAIN (attributes) = NULL_TREE;
23507 }
23508
23509 /* If there is any qualification still in effect, clear it
23510 now; we will be starting fresh with the next declarator. */
23511 parser->scope = NULL_TREE;
23512 parser->qualifying_scope = NULL_TREE;
23513 parser->object_scope = NULL_TREE;
23514 /* If it's a `,', then there are more declarators. */
23515 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23516 {
23517 cp_lexer_consume_token (parser->lexer);
23518 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23519 {
23520 cp_token *token = cp_lexer_previous_token (parser->lexer);
23521 error_at (token->location,
23522 "stray %<,%> at end of member declaration");
23523 }
23524 }
23525 /* If the next token isn't a `;', then we have a parse error. */
23526 else if (cp_lexer_next_token_is_not (parser->lexer,
23527 CPP_SEMICOLON))
23528 {
23529 /* The next token might be a ways away from where the
23530 actual semicolon is missing. Find the previous token
23531 and use that for our error position. */
23532 cp_token *token = cp_lexer_previous_token (parser->lexer);
23533 error_at (token->location,
23534 "expected %<;%> at end of member declaration");
23535
23536 /* Assume that the user meant to provide a semicolon. If
23537 we were to cp_parser_skip_to_end_of_statement, we might
23538 skip to a semicolon inside a member function definition
23539 and issue nonsensical error messages. */
23540 assume_semicolon = true;
23541 }
23542
23543 if (decl)
23544 {
23545 /* Add DECL to the list of members. */
23546 if (!friend_p
23547 /* Explicitly include, eg, NSDMIs, for better error
23548 recovery (c++/58650). */
23549 || !DECL_DECLARES_FUNCTION_P (decl))
23550 finish_member_declaration (decl);
23551
23552 if (TREE_CODE (decl) == FUNCTION_DECL)
23553 cp_parser_save_default_args (parser, decl);
23554 else if (TREE_CODE (decl) == FIELD_DECL
23555 && !DECL_C_BIT_FIELD (decl)
23556 && DECL_INITIAL (decl))
23557 /* Add DECL to the queue of NSDMI to be parsed later. */
23558 vec_safe_push (unparsed_nsdmis, decl);
23559 }
23560
23561 if (assume_semicolon)
23562 goto out;
23563 }
23564 }
23565
23566 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23567 out:
23568 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23569}
23570
23571/* Parse a pure-specifier.
23572
23573 pure-specifier:
23574 = 0
23575
23576 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23577 Otherwise, ERROR_MARK_NODE is returned. */
23578
23579static tree
23580cp_parser_pure_specifier (cp_parser* parser)
23581{
23582 cp_token *token;
23583
23584 /* Look for the `=' token. */
23585 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23586 return error_mark_node;
23587 /* Look for the `0' token. */
23588 token = cp_lexer_peek_token (parser->lexer);
23589
23590 if (token->type == CPP_EOF
23591 || token->type == CPP_PRAGMA_EOL)
23592 return error_mark_node;
23593
23594 cp_lexer_consume_token (parser->lexer);
23595
23596 /* Accept = default or = delete in c++0x mode. */
23597 if (token->keyword == RID_DEFAULT
23598 || token->keyword == RID_DELETE)
23599 {
23600 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23601 return token->u.value;
23602 }
23603
23604 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23605 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23606 {
23607 cp_parser_error (parser,
23608 "invalid pure specifier (only %<= 0%> is allowed)");
23609 cp_parser_skip_to_end_of_statement (parser);
23610 return error_mark_node;
23611 }
23612 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23613 {
23614 error_at (token->location, "templates may not be %<virtual%>");
23615 return error_mark_node;
23616 }
23617
23618 return integer_zero_node;
23619}
23620
23621/* Parse a constant-initializer.
23622
23623 constant-initializer:
23624 = constant-expression
23625
23626 Returns a representation of the constant-expression. */
23627
23628static tree
23629cp_parser_constant_initializer (cp_parser* parser)
23630{
23631 /* Look for the `=' token. */
23632 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23633 return error_mark_node;
23634
23635 /* It is invalid to write:
23636
23637 struct S { static const int i = { 7 }; };
23638
23639 */
23640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23641 {
23642 cp_parser_error (parser,
23643 "a brace-enclosed initializer is not allowed here");
23644 /* Consume the opening brace. */
23645 cp_lexer_consume_token (parser->lexer);
23646 /* Skip the initializer. */
23647 cp_parser_skip_to_closing_brace (parser);
23648 /* Look for the trailing `}'. */
23649 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23650
23651 return error_mark_node;
23652 }
23653
23654 return cp_parser_constant_expression (parser);
23655}
23656
23657/* Derived classes [gram.class.derived] */
23658
23659/* Parse a base-clause.
23660
23661 base-clause:
23662 : base-specifier-list
23663
23664 base-specifier-list:
23665 base-specifier ... [opt]
23666 base-specifier-list , base-specifier ... [opt]
23667
23668 Returns a TREE_LIST representing the base-classes, in the order in
23669 which they were declared. The representation of each node is as
23670 described by cp_parser_base_specifier.
23671
23672 In the case that no bases are specified, this function will return
23673 NULL_TREE, not ERROR_MARK_NODE. */
23674
23675static tree
23676cp_parser_base_clause (cp_parser* parser)
23677{
23678 tree bases = NULL_TREE;
23679
23680 /* Look for the `:' that begins the list. */
23681 cp_parser_require (parser, CPP_COLON, RT_COLON);
23682
23683 /* Scan the base-specifier-list. */
23684 while (true)
23685 {
23686 cp_token *token;
23687 tree base;
23688 bool pack_expansion_p = false;
23689
23690 /* Look for the base-specifier. */
23691 base = cp_parser_base_specifier (parser);
23692 /* Look for the (optional) ellipsis. */
23693 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23694 {
23695 /* Consume the `...'. */
23696 cp_lexer_consume_token (parser->lexer);
23697
23698 pack_expansion_p = true;
23699 }
23700
23701 /* Add BASE to the front of the list. */
23702 if (base && base != error_mark_node)
23703 {
23704 if (pack_expansion_p)
23705 /* Make this a pack expansion type. */
23706 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23707
23708 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23709 {
23710 TREE_CHAIN (base) = bases;
23711 bases = base;
23712 }
23713 }
23714 /* Peek at the next token. */
23715 token = cp_lexer_peek_token (parser->lexer);
23716 /* If it's not a comma, then the list is complete. */
23717 if (token->type != CPP_COMMA)
23718 break;
23719 /* Consume the `,'. */
23720 cp_lexer_consume_token (parser->lexer);
23721 }
23722
23723 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23724 base class had a qualified name. However, the next name that
23725 appears is certainly not qualified. */
23726 parser->scope = NULL_TREE;
23727 parser->qualifying_scope = NULL_TREE;
23728 parser->object_scope = NULL_TREE;
23729
23730 return nreverse (bases);
23731}
23732
23733/* Parse a base-specifier.
23734
23735 base-specifier:
23736 :: [opt] nested-name-specifier [opt] class-name
23737 virtual access-specifier [opt] :: [opt] nested-name-specifier
23738 [opt] class-name
23739 access-specifier virtual [opt] :: [opt] nested-name-specifier
23740 [opt] class-name
23741
23742 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23743 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23744 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23745 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23746
23747static tree
23748cp_parser_base_specifier (cp_parser* parser)
23749{
23750 cp_token *token;
23751 bool done = false;
23752 bool virtual_p = false;
23753 bool duplicate_virtual_error_issued_p = false;
23754 bool duplicate_access_error_issued_p = false;
23755 bool class_scope_p, template_p;
23756 tree access = access_default_node;
23757 tree type;
23758
23759 /* Process the optional `virtual' and `access-specifier'. */
23760 while (!done)
23761 {
23762 /* Peek at the next token. */
23763 token = cp_lexer_peek_token (parser->lexer);
23764 /* Process `virtual'. */
23765 switch (token->keyword)
23766 {
23767 case RID_VIRTUAL:
23768 /* If `virtual' appears more than once, issue an error. */
23769 if (virtual_p && !duplicate_virtual_error_issued_p)
23770 {
23771 cp_parser_error (parser,
23772 "%<virtual%> specified more than once in base-specifier");
23773 duplicate_virtual_error_issued_p = true;
23774 }
23775
23776 virtual_p = true;
23777
23778 /* Consume the `virtual' token. */
23779 cp_lexer_consume_token (parser->lexer);
23780
23781 break;
23782
23783 case RID_PUBLIC:
23784 case RID_PROTECTED:
23785 case RID_PRIVATE:
23786 /* If more than one access specifier appears, issue an
23787 error. */
23788 if (access != access_default_node
23789 && !duplicate_access_error_issued_p)
23790 {
23791 cp_parser_error (parser,
23792 "more than one access specifier in base-specifier");
23793 duplicate_access_error_issued_p = true;
23794 }
23795
23796 access = ridpointers[(int) token->keyword];
23797
23798 /* Consume the access-specifier. */
23799 cp_lexer_consume_token (parser->lexer);
23800
23801 break;
23802
23803 default:
23804 done = true;
23805 break;
23806 }
23807 }
23808 /* It is not uncommon to see programs mechanically, erroneously, use
23809 the 'typename' keyword to denote (dependent) qualified types
23810 as base classes. */
23811 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23812 {
23813 token = cp_lexer_peek_token (parser->lexer);
23814 if (!processing_template_decl)
23815 error_at (token->location,
23816 "keyword %<typename%> not allowed outside of templates");
23817 else
23818 error_at (token->location,
23819 "keyword %<typename%> not allowed in this context "
23820 "(the base class is implicitly a type)");
23821 cp_lexer_consume_token (parser->lexer);
23822 }
23823
23824 /* Look for the optional `::' operator. */
23825 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23826 /* Look for the nested-name-specifier. The simplest way to
23827 implement:
23828
23829 [temp.res]
23830
23831 The keyword `typename' is not permitted in a base-specifier or
23832 mem-initializer; in these contexts a qualified name that
23833 depends on a template-parameter is implicitly assumed to be a
23834 type name.
23835
23836 is to pretend that we have seen the `typename' keyword at this
23837 point. */
23838 cp_parser_nested_name_specifier_opt (parser,
23839 /*typename_keyword_p=*/true,
23840 /*check_dependency_p=*/true,
23841 /*type_p=*/true,
23842 /*is_declaration=*/true);
23843 /* If the base class is given by a qualified name, assume that names
23844 we see are type names or templates, as appropriate. */
23845 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23846 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23847
23848 if (!parser->scope
23849 && cp_lexer_next_token_is_decltype (parser->lexer))
23850 /* DR 950 allows decltype as a base-specifier. */
23851 type = cp_parser_decltype (parser);
23852 else
23853 {
23854 /* Otherwise, look for the class-name. */
23855 type = cp_parser_class_name (parser,
23856 class_scope_p,
23857 template_p,
23858 typename_type,
23859 /*check_dependency_p=*/true,
23860 /*class_head_p=*/false,
23861 /*is_declaration=*/true);
23862 type = TREE_TYPE (type);
23863 }
23864
23865 if (type == error_mark_node)
23866 return error_mark_node;
23867
23868 return finish_base_specifier (type, access, virtual_p);
23869}
23870
23871/* Exception handling [gram.exception] */
23872
23873/* Parse an (optional) noexcept-specification.
23874
23875 noexcept-specification:
23876 noexcept ( constant-expression ) [opt]
23877
23878 If no noexcept-specification is present, returns NULL_TREE.
23879 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23880 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23881 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23882 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23883 in which case a boolean condition is returned instead. */
23884
23885static tree
23886cp_parser_noexcept_specification_opt (cp_parser* parser,
23887 bool require_constexpr,
23888 bool* consumed_expr,
23889 bool return_cond)
23890{
23891 cp_token *token;
23892 const char *saved_message;
23893
23894 /* Peek at the next token. */
23895 token = cp_lexer_peek_token (parser->lexer);
23896
23897 /* Is it a noexcept-specification? */
23898 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23899 {
23900 tree expr;
23901 cp_lexer_consume_token (parser->lexer);
23902
23903 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23904 {
23905 cp_lexer_consume_token (parser->lexer);
23906
23907 if (require_constexpr)
23908 {
23909 /* Types may not be defined in an exception-specification. */
23910 saved_message = parser->type_definition_forbidden_message;
23911 parser->type_definition_forbidden_message
23912 = G_("types may not be defined in an exception-specification");
23913
23914 expr = cp_parser_constant_expression (parser);
23915
23916 /* Restore the saved message. */
23917 parser->type_definition_forbidden_message = saved_message;
23918 }
23919 else
23920 {
23921 expr = cp_parser_expression (parser);
23922 *consumed_expr = true;
23923 }
23924
23925 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23926 }
23927 else
23928 {
23929 expr = boolean_true_node;
23930 if (!require_constexpr)
23931 *consumed_expr = false;
23932 }
23933
23934 /* We cannot build a noexcept-spec right away because this will check
23935 that expr is a constexpr. */
23936 if (!return_cond)
23937 return build_noexcept_spec (expr, tf_warning_or_error);
23938 else
23939 return expr;
23940 }
23941 else
23942 return NULL_TREE;
23943}
23944
23945/* Parse an (optional) exception-specification.
23946
23947 exception-specification:
23948 throw ( type-id-list [opt] )
23949
23950 Returns a TREE_LIST representing the exception-specification. The
23951 TREE_VALUE of each node is a type. */
23952
23953static tree
23954cp_parser_exception_specification_opt (cp_parser* parser)
23955{
23956 cp_token *token;
23957 tree type_id_list;
23958 const char *saved_message;
23959
23960 /* Peek at the next token. */
23961 token = cp_lexer_peek_token (parser->lexer);
23962
23963 /* Is it a noexcept-specification? */
23964 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23965 false);
23966 if (type_id_list != NULL_TREE)
23967 return type_id_list;
23968
23969 /* If it's not `throw', then there's no exception-specification. */
23970 if (!cp_parser_is_keyword (token, RID_THROW))
23971 return NULL_TREE;
23972
23973 location_t loc = token->location;
23974
23975 /* Consume the `throw'. */
23976 cp_lexer_consume_token (parser->lexer);
23977
23978 /* Look for the `('. */
23979 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23980
23981 /* Peek at the next token. */
23982 token = cp_lexer_peek_token (parser->lexer);
23983 /* If it's not a `)', then there is a type-id-list. */
23984 if (token->type != CPP_CLOSE_PAREN)
23985 {
23986 /* Types may not be defined in an exception-specification. */
23987 saved_message = parser->type_definition_forbidden_message;
23988 parser->type_definition_forbidden_message
23989 = G_("types may not be defined in an exception-specification");
23990 /* Parse the type-id-list. */
23991 type_id_list = cp_parser_type_id_list (parser);
23992 /* Restore the saved message. */
23993 parser->type_definition_forbidden_message = saved_message;
23994
23995 if (cxx_dialect >= cxx1z)
23996 {
23997 error_at (loc, "ISO C++1z does not allow dynamic exception "
23998 "specifications");
23999 type_id_list = NULL_TREE;
24000 }
24001 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24002 warning_at (loc, OPT_Wdeprecated,
24003 "dynamic exception specifications are deprecated in "
24004 "C++11");
24005 }
24006 /* In C++17, throw() is equivalent to noexcept (true). throw()
24007 is deprecated in C++11 and above as well, but is still widely used,
24008 so don't warn about it yet. */
24009 else if (cxx_dialect >= cxx1z)
24010 type_id_list = noexcept_true_spec;
24011 else
24012 type_id_list = empty_except_spec;
24013
24014 /* Look for the `)'. */
24015 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24016
24017 return type_id_list;
24018}
24019
24020/* Parse an (optional) type-id-list.
24021
24022 type-id-list:
24023 type-id ... [opt]
24024 type-id-list , type-id ... [opt]
24025
24026 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24027 in the order that the types were presented. */
24028
24029static tree
24030cp_parser_type_id_list (cp_parser* parser)
24031{
24032 tree types = NULL_TREE;
24033
24034 while (true)
24035 {
24036 cp_token *token;
24037 tree type;
24038
24039 token = cp_lexer_peek_token (parser->lexer);
24040
24041 /* Get the next type-id. */
24042 type = cp_parser_type_id (parser);
24043 /* Check for invalid 'auto'. */
24044 if (flag_concepts && type_uses_auto (type))
24045 {
24046 error_at (token->location,
24047 "invalid use of %<auto%> in exception-specification");
24048 type = error_mark_node;
24049 }
24050 /* Parse the optional ellipsis. */
24051 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24052 {
24053 /* Consume the `...'. */
24054 cp_lexer_consume_token (parser->lexer);
24055
24056 /* Turn the type into a pack expansion expression. */
24057 type = make_pack_expansion (type);
24058 }
24059 /* Add it to the list. */
24060 types = add_exception_specifier (types, type, /*complain=*/1);
24061 /* Peek at the next token. */
24062 token = cp_lexer_peek_token (parser->lexer);
24063 /* If it is not a `,', we are done. */
24064 if (token->type != CPP_COMMA)
24065 break;
24066 /* Consume the `,'. */
24067 cp_lexer_consume_token (parser->lexer);
24068 }
24069
24070 return nreverse (types);
24071}
24072
24073/* Parse a try-block.
24074
24075 try-block:
24076 try compound-statement handler-seq */
24077
24078static tree
24079cp_parser_try_block (cp_parser* parser)
24080{
24081 tree try_block;
24082
24083 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24084 if (parser->in_function_body
24085 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24086 error ("%<try%> in %<constexpr%> function");
24087
24088 try_block = begin_try_block ();
24089 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24090 finish_try_block (try_block);
24091 cp_parser_handler_seq (parser);
24092 finish_handler_sequence (try_block);
24093
24094 return try_block;
24095}
24096
24097/* Parse a function-try-block.
24098
24099 function-try-block:
24100 try ctor-initializer [opt] function-body handler-seq */
24101
24102static bool
24103cp_parser_function_try_block (cp_parser* parser)
24104{
24105 tree compound_stmt;
24106 tree try_block;
24107 bool ctor_initializer_p;
24108
24109 /* Look for the `try' keyword. */
24110 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24111 return false;
24112 /* Let the rest of the front end know where we are. */
24113 try_block = begin_function_try_block (&compound_stmt);
24114 /* Parse the function-body. */
24115 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24116 (parser, /*in_function_try_block=*/true);
24117 /* We're done with the `try' part. */
24118 finish_function_try_block (try_block);
24119 /* Parse the handlers. */
24120 cp_parser_handler_seq (parser);
24121 /* We're done with the handlers. */
24122 finish_function_handler_sequence (try_block, compound_stmt);
24123
24124 return ctor_initializer_p;
24125}
24126
24127/* Parse a handler-seq.
24128
24129 handler-seq:
24130 handler handler-seq [opt] */
24131
24132static void
24133cp_parser_handler_seq (cp_parser* parser)
24134{
24135 while (true)
24136 {
24137 cp_token *token;
24138
24139 /* Parse the handler. */
24140 cp_parser_handler (parser);
24141 /* Peek at the next token. */
24142 token = cp_lexer_peek_token (parser->lexer);
24143 /* If it's not `catch' then there are no more handlers. */
24144 if (!cp_parser_is_keyword (token, RID_CATCH))
24145 break;
24146 }
24147}
24148
24149/* Parse a handler.
24150
24151 handler:
24152 catch ( exception-declaration ) compound-statement */
24153
24154static void
24155cp_parser_handler (cp_parser* parser)
24156{
24157 tree handler;
24158 tree declaration;
24159
24160 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24161 handler = begin_handler ();
24162 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24163 declaration = cp_parser_exception_declaration (parser);
24164 finish_handler_parms (declaration, handler);
24165 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24166 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24167 finish_handler (handler);
24168}
24169
24170/* Parse an exception-declaration.
24171
24172 exception-declaration:
24173 type-specifier-seq declarator
24174 type-specifier-seq abstract-declarator
24175 type-specifier-seq
24176 ...
24177
24178 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24179 ellipsis variant is used. */
24180
24181static tree
24182cp_parser_exception_declaration (cp_parser* parser)
24183{
24184 cp_decl_specifier_seq type_specifiers;
24185 cp_declarator *declarator;
24186 const char *saved_message;
24187
24188 /* If it's an ellipsis, it's easy to handle. */
24189 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24190 {
24191 /* Consume the `...' token. */
24192 cp_lexer_consume_token (parser->lexer);
24193 return NULL_TREE;
24194 }
24195
24196 /* Types may not be defined in exception-declarations. */
24197 saved_message = parser->type_definition_forbidden_message;
24198 parser->type_definition_forbidden_message
24199 = G_("types may not be defined in exception-declarations");
24200
24201 /* Parse the type-specifier-seq. */
24202 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24203 /*is_trailing_return=*/false,
24204 &type_specifiers);
24205 /* If it's a `)', then there is no declarator. */
24206 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24207 declarator = NULL;
24208 else
24209 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24210 /*ctor_dtor_or_conv_p=*/NULL,
24211 /*parenthesized_p=*/NULL,
24212 /*member_p=*/false,
24213 /*friend_p=*/false);
24214
24215 /* Restore the saved message. */
24216 parser->type_definition_forbidden_message = saved_message;
24217
24218 if (!type_specifiers.any_specifiers_p)
24219 return error_mark_node;
24220
24221 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24222}
24223
24224/* Parse a throw-expression.
24225
24226 throw-expression:
24227 throw assignment-expression [opt]
24228
24229 Returns a THROW_EXPR representing the throw-expression. */
24230
24231static tree
24232cp_parser_throw_expression (cp_parser* parser)
24233{
24234 tree expression;
24235 cp_token* token;
24236
24237 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24238 token = cp_lexer_peek_token (parser->lexer);
24239 /* Figure out whether or not there is an assignment-expression
24240 following the "throw" keyword. */
24241 if (token->type == CPP_COMMA
24242 || token->type == CPP_SEMICOLON
24243 || token->type == CPP_CLOSE_PAREN
24244 || token->type == CPP_CLOSE_SQUARE
24245 || token->type == CPP_CLOSE_BRACE
24246 || token->type == CPP_COLON)
24247 expression = NULL_TREE;
24248 else
24249 expression = cp_parser_assignment_expression (parser);
24250
24251 return build_throw (expression);
24252}
24253
24254/* GNU Extensions */
24255
24256/* Parse an (optional) asm-specification.
24257
24258 asm-specification:
24259 asm ( string-literal )
24260
24261 If the asm-specification is present, returns a STRING_CST
24262 corresponding to the string-literal. Otherwise, returns
24263 NULL_TREE. */
24264
24265static tree
24266cp_parser_asm_specification_opt (cp_parser* parser)
24267{
24268 cp_token *token;
24269 tree asm_specification;
24270
24271 /* Peek at the next token. */
24272 token = cp_lexer_peek_token (parser->lexer);
24273 /* If the next token isn't the `asm' keyword, then there's no
24274 asm-specification. */
24275 if (!cp_parser_is_keyword (token, RID_ASM))
24276 return NULL_TREE;
24277
24278 /* Consume the `asm' token. */
24279 cp_lexer_consume_token (parser->lexer);
24280 /* Look for the `('. */
24281 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24282
24283 /* Look for the string-literal. */
24284 asm_specification = cp_parser_string_literal (parser, false, false);
24285
24286 /* Look for the `)'. */
24287 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24288
24289 return asm_specification;
24290}
24291
24292/* Parse an asm-operand-list.
24293
24294 asm-operand-list:
24295 asm-operand
24296 asm-operand-list , asm-operand
24297
24298 asm-operand:
24299 string-literal ( expression )
24300 [ string-literal ] string-literal ( expression )
24301
24302 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24303 each node is the expression. The TREE_PURPOSE is itself a
24304 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24305 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24306 is a STRING_CST for the string literal before the parenthesis. Returns
24307 ERROR_MARK_NODE if any of the operands are invalid. */
24308
24309static tree
24310cp_parser_asm_operand_list (cp_parser* parser)
24311{
24312 tree asm_operands = NULL_TREE;
24313 bool invalid_operands = false;
24314
24315 while (true)
24316 {
24317 tree string_literal;
24318 tree expression;
24319 tree name;
24320
24321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24322 {
24323 /* Consume the `[' token. */
24324 cp_lexer_consume_token (parser->lexer);
24325 /* Read the operand name. */
24326 name = cp_parser_identifier (parser);
24327 if (name != error_mark_node)
24328 name = build_string (IDENTIFIER_LENGTH (name),
24329 IDENTIFIER_POINTER (name));
24330 /* Look for the closing `]'. */
24331 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24332 }
24333 else
24334 name = NULL_TREE;
24335 /* Look for the string-literal. */
24336 string_literal = cp_parser_string_literal (parser, false, false);
24337
24338 /* Look for the `('. */
24339 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24340 /* Parse the expression. */
24341 expression = cp_parser_expression (parser);
24342 /* Look for the `)'. */
24343 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24344
24345 if (name == error_mark_node
24346 || string_literal == error_mark_node
24347 || expression == error_mark_node)
24348 invalid_operands = true;
24349
24350 /* Add this operand to the list. */
24351 asm_operands = tree_cons (build_tree_list (name, string_literal),
24352 expression,
24353 asm_operands);
24354 /* If the next token is not a `,', there are no more
24355 operands. */
24356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24357 break;
24358 /* Consume the `,'. */
24359 cp_lexer_consume_token (parser->lexer);
24360 }
24361
24362 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24363}
24364
24365/* Parse an asm-clobber-list.
24366
24367 asm-clobber-list:
24368 string-literal
24369 asm-clobber-list , string-literal
24370
24371 Returns a TREE_LIST, indicating the clobbers in the order that they
24372 appeared. The TREE_VALUE of each node is a STRING_CST. */
24373
24374static tree
24375cp_parser_asm_clobber_list (cp_parser* parser)
24376{
24377 tree clobbers = NULL_TREE;
24378
24379 while (true)
24380 {
24381 tree string_literal;
24382
24383 /* Look for the string literal. */
24384 string_literal = cp_parser_string_literal (parser, false, false);
24385 /* Add it to the list. */
24386 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24387 /* If the next token is not a `,', then the list is
24388 complete. */
24389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24390 break;
24391 /* Consume the `,' token. */
24392 cp_lexer_consume_token (parser->lexer);
24393 }
24394
24395 return clobbers;
24396}
24397
24398/* Parse an asm-label-list.
24399
24400 asm-label-list:
24401 identifier
24402 asm-label-list , identifier
24403
24404 Returns a TREE_LIST, indicating the labels in the order that they
24405 appeared. The TREE_VALUE of each node is a label. */
24406
24407static tree
24408cp_parser_asm_label_list (cp_parser* parser)
24409{
24410 tree labels = NULL_TREE;
24411
24412 while (true)
24413 {
24414 tree identifier, label, name;
24415
24416 /* Look for the identifier. */
24417 identifier = cp_parser_identifier (parser);
24418 if (!error_operand_p (identifier))
24419 {
24420 label = lookup_label (identifier);
24421 if (TREE_CODE (label) == LABEL_DECL)
24422 {
24423 TREE_USED (label) = 1;
24424 check_goto (label);
24425 name = build_string (IDENTIFIER_LENGTH (identifier),
24426 IDENTIFIER_POINTER (identifier));
24427 labels = tree_cons (name, label, labels);
24428 }
24429 }
24430 /* If the next token is not a `,', then the list is
24431 complete. */
24432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24433 break;
24434 /* Consume the `,' token. */
24435 cp_lexer_consume_token (parser->lexer);
24436 }
24437
24438 return nreverse (labels);
24439}
24440
24441/* Return TRUE iff the next tokens in the stream are possibly the
24442 beginning of a GNU extension attribute. */
24443
24444static bool
24445cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24446{
24447 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24448}
24449
24450/* Return TRUE iff the next tokens in the stream are possibly the
24451 beginning of a standard C++-11 attribute specifier. */
24452
24453static bool
24454cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24455{
24456 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24457}
24458
24459/* Return TRUE iff the next Nth tokens in the stream are possibly the
24460 beginning of a standard C++-11 attribute specifier. */
24461
24462static bool
24463cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24464{
24465 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24466
24467 return (cxx_dialect >= cxx11
24468 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24469 || (token->type == CPP_OPEN_SQUARE
24470 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24471 && token->type == CPP_OPEN_SQUARE)));
24472}
24473
24474/* Return TRUE iff the next Nth tokens in the stream are possibly the
24475 beginning of a GNU extension attribute. */
24476
24477static bool
24478cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24479{
24480 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24481
24482 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24483}
24484
24485/* Return true iff the next tokens can be the beginning of either a
24486 GNU attribute list, or a standard C++11 attribute sequence. */
24487
24488static bool
24489cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24490{
24491 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24492 || cp_next_tokens_can_be_std_attribute_p (parser));
24493}
24494
24495/* Return true iff the next Nth tokens can be the beginning of either
24496 a GNU attribute list, or a standard C++11 attribute sequence. */
24497
24498static bool
24499cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24500{
24501 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24502 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24503}
24504
24505/* Parse either a standard C++-11 attribute-specifier-seq, or a series
24506 of GNU attributes, or return NULL. */
24507
24508static tree
24509cp_parser_attributes_opt (cp_parser *parser)
24510{
24511 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24512 return cp_parser_gnu_attributes_opt (parser);
24513 return cp_parser_std_attribute_spec_seq (parser);
24514}
24515
24516#define CILK_SIMD_FN_CLAUSE_MASK \
24517 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24518 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24519 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24520 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24521 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24522
24523/* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24524 vector [(<clauses>)] */
24525
24526static void
24527cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24528{
24529 bool first_p = parser->cilk_simd_fn_info == NULL;
24530 cp_token *token = v_token;
24531 if (first_p)
24532 {
24533 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24534 parser->cilk_simd_fn_info->error_seen = false;
24535 parser->cilk_simd_fn_info->fndecl_seen = false;
24536 parser->cilk_simd_fn_info->tokens = vNULL;
24537 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24538 }
24539 int paren_scope = 0;
24540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24541 {
24542 cp_lexer_consume_token (parser->lexer);
24543 v_token = cp_lexer_peek_token (parser->lexer);
24544 paren_scope++;
24545 }
24546 while (paren_scope > 0)
24547 {
24548 token = cp_lexer_peek_token (parser->lexer);
24549 if (token->type == CPP_OPEN_PAREN)
24550 paren_scope++;
24551 else if (token->type == CPP_CLOSE_PAREN)
24552 paren_scope--;
24553 /* Do not push the last ')' */
24554 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24555 cp_lexer_consume_token (parser->lexer);
24556 }
24557
24558 token->type = CPP_PRAGMA_EOL;
24559 parser->lexer->next_token = token;
24560 cp_lexer_consume_token (parser->lexer);
24561
24562 struct cp_token_cache *cp
24563 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24564 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24565}
24566
24567/* Parse an (optional) series of attributes.
24568
24569 attributes:
24570 attributes attribute
24571
24572 attribute:
24573 __attribute__ (( attribute-list [opt] ))
24574
24575 The return value is as for cp_parser_gnu_attribute_list. */
24576
24577static tree
24578cp_parser_gnu_attributes_opt (cp_parser* parser)
24579{
24580 tree attributes = NULL_TREE;
24581
24582 while (true)
24583 {
24584 cp_token *token;
24585 tree attribute_list;
24586 bool ok = true;
24587
24588 /* Peek at the next token. */
24589 token = cp_lexer_peek_token (parser->lexer);
24590 /* If it's not `__attribute__', then we're done. */
24591 if (token->keyword != RID_ATTRIBUTE)
24592 break;
24593
24594 /* Consume the `__attribute__' keyword. */
24595 cp_lexer_consume_token (parser->lexer);
24596 /* Look for the two `(' tokens. */
24597 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24598 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24599
24600 /* Peek at the next token. */
24601 token = cp_lexer_peek_token (parser->lexer);
24602 if (token->type != CPP_CLOSE_PAREN)
24603 /* Parse the attribute-list. */
24604 attribute_list = cp_parser_gnu_attribute_list (parser);
24605 else
24606 /* If the next token is a `)', then there is no attribute
24607 list. */
24608 attribute_list = NULL;
24609
24610 /* Look for the two `)' tokens. */
24611 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24612 ok = false;
24613 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24614 ok = false;
24615 if (!ok)
24616 cp_parser_skip_to_end_of_statement (parser);
24617
24618 /* Add these new attributes to the list. */
24619 attributes = attr_chainon (attributes, attribute_list);
24620 }
24621
24622 return attributes;
24623}
24624
24625/* Parse a GNU attribute-list.
24626
24627 attribute-list:
24628 attribute
24629 attribute-list , attribute
24630
24631 attribute:
24632 identifier
24633 identifier ( identifier )
24634 identifier ( identifier , expression-list )
24635 identifier ( expression-list )
24636
24637 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24638 to an attribute. The TREE_PURPOSE of each node is the identifier
24639 indicating which attribute is in use. The TREE_VALUE represents
24640 the arguments, if any. */
24641
24642static tree
24643cp_parser_gnu_attribute_list (cp_parser* parser)
24644{
24645 tree attribute_list = NULL_TREE;
24646 bool save_translate_strings_p = parser->translate_strings_p;
24647
24648 parser->translate_strings_p = false;
24649 while (true)
24650 {
24651 cp_token *token;
24652 tree identifier;
24653 tree attribute;
24654
24655 /* Look for the identifier. We also allow keywords here; for
24656 example `__attribute__ ((const))' is legal. */
24657 token = cp_lexer_peek_token (parser->lexer);
24658 if (token->type == CPP_NAME
24659 || token->type == CPP_KEYWORD)
24660 {
24661 tree arguments = NULL_TREE;
24662
24663 /* Consume the token, but save it since we need it for the
24664 SIMD enabled function parsing. */
24665 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24666
24667 /* Save away the identifier that indicates which attribute
24668 this is. */
24669 identifier = (token->type == CPP_KEYWORD)
24670 /* For keywords, use the canonical spelling, not the
24671 parsed identifier. */
24672 ? ridpointers[(int) token->keyword]
24673 : id_token->u.value;
24674
24675 attribute = build_tree_list (identifier, NULL_TREE);
24676
24677 /* Peek at the next token. */
24678 token = cp_lexer_peek_token (parser->lexer);
24679 /* If it's an `(', then parse the attribute arguments. */
24680 if (token->type == CPP_OPEN_PAREN)
24681 {
24682 vec<tree, va_gc> *vec;
24683 int attr_flag = (attribute_takes_identifier_p (identifier)
24684 ? id_attr : normal_attr);
24685 if (is_cilkplus_vector_p (identifier))
24686 {
24687 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24688 continue;
24689 }
24690 else
24691 vec = cp_parser_parenthesized_expression_list
24692 (parser, attr_flag, /*cast_p=*/false,
24693 /*allow_expansion_p=*/false,
24694 /*non_constant_p=*/NULL);
24695 if (vec == NULL)
24696 arguments = error_mark_node;
24697 else
24698 {
24699 arguments = build_tree_list_vec (vec);
24700 release_tree_vector (vec);
24701 }
24702 /* Save the arguments away. */
24703 TREE_VALUE (attribute) = arguments;
24704 }
24705 else if (is_cilkplus_vector_p (identifier))
24706 {
24707 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24708 continue;
24709 }
24710
24711 if (arguments != error_mark_node)
24712 {
24713 /* Add this attribute to the list. */
24714 TREE_CHAIN (attribute) = attribute_list;
24715 attribute_list = attribute;
24716 }
24717
24718 token = cp_lexer_peek_token (parser->lexer);
24719 }
24720 /* Now, look for more attributes. If the next token isn't a
24721 `,', we're done. */
24722 if (token->type != CPP_COMMA)
24723 break;
24724
24725 /* Consume the comma and keep going. */
24726 cp_lexer_consume_token (parser->lexer);
24727 }
24728 parser->translate_strings_p = save_translate_strings_p;
24729
24730 /* We built up the list in reverse order. */
24731 return nreverse (attribute_list);
24732}
24733
24734/* Parse a standard C++11 attribute.
24735
24736 The returned representation is a TREE_LIST which TREE_PURPOSE is
24737 the scoped name of the attribute, and the TREE_VALUE is its
24738 arguments list.
24739
24740 Note that the scoped name of the attribute is itself a TREE_LIST
24741 which TREE_PURPOSE is the namespace of the attribute, and
24742 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24743 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24744 and which TREE_PURPOSE is directly the attribute name.
24745
24746 Clients of the attribute code should use get_attribute_namespace
24747 and get_attribute_name to get the actual namespace and name of
24748 attributes, regardless of their being GNU or C++11 attributes.
24749
24750 attribute:
24751 attribute-token attribute-argument-clause [opt]
24752
24753 attribute-token:
24754 identifier
24755 attribute-scoped-token
24756
24757 attribute-scoped-token:
24758 attribute-namespace :: identifier
24759
24760 attribute-namespace:
24761 identifier
24762
24763 attribute-argument-clause:
24764 ( balanced-token-seq )
24765
24766 balanced-token-seq:
24767 balanced-token [opt]
24768 balanced-token-seq balanced-token
24769
24770 balanced-token:
24771 ( balanced-token-seq )
24772 [ balanced-token-seq ]
24773 { balanced-token-seq }. */
24774
24775static tree
24776cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24777{
24778 tree attribute, attr_id = NULL_TREE, arguments;
24779 cp_token *token;
24780
24781 /* First, parse name of the attribute, a.k.a attribute-token. */
24782
24783 token = cp_lexer_peek_token (parser->lexer);
24784 if (token->type == CPP_NAME)
24785 attr_id = token->u.value;
24786 else if (token->type == CPP_KEYWORD)
24787 attr_id = ridpointers[(int) token->keyword];
24788 else if (token->flags & NAMED_OP)
24789 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24790
24791 if (attr_id == NULL_TREE)
24792 return NULL_TREE;
24793
24794 cp_lexer_consume_token (parser->lexer);
24795
24796 token = cp_lexer_peek_token (parser->lexer);
24797 if (token->type == CPP_SCOPE)
24798 {
24799 /* We are seeing a scoped attribute token. */
24800
24801 cp_lexer_consume_token (parser->lexer);
24802 if (attr_ns)
24803 error_at (token->location, "attribute using prefix used together "
24804 "with scoped attribute token");
24805 attr_ns = attr_id;
24806
24807 token = cp_lexer_consume_token (parser->lexer);
24808 if (token->type == CPP_NAME)
24809 attr_id = token->u.value;
24810 else if (token->type == CPP_KEYWORD)
24811 attr_id = ridpointers[(int) token->keyword];
24812 else if (token->flags & NAMED_OP)
24813 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24814 else
24815 {
24816 error_at (token->location,
24817 "expected an identifier for the attribute name");
24818 return error_mark_node;
24819 }
24820 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24821 NULL_TREE);
24822 token = cp_lexer_peek_token (parser->lexer);
24823 }
24824 else if (attr_ns)
24825 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24826 NULL_TREE);
24827 else
24828 {
24829 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24830 NULL_TREE);
24831 /* C++11 noreturn attribute is equivalent to GNU's. */
24832 if (is_attribute_p ("noreturn", attr_id))
24833 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24834 /* C++14 deprecated attribute is equivalent to GNU's. */
24835 else if (is_attribute_p ("deprecated", attr_id))
24836 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24837 /* C++17 fallthrough attribute is equivalent to GNU's. */
24838 else if (is_attribute_p ("fallthrough", attr_id))
24839 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24840 /* Transactional Memory TS optimize_for_synchronized attribute is
24841 equivalent to GNU transaction_callable. */
24842 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24843 TREE_PURPOSE (attribute)
24844 = get_identifier ("transaction_callable");
24845 /* Transactional Memory attributes are GNU attributes. */
24846 else if (tm_attr_to_mask (attr_id))
24847 TREE_PURPOSE (attribute) = attr_id;
24848 }
24849
24850 /* Now parse the optional argument clause of the attribute. */
24851
24852 if (token->type != CPP_OPEN_PAREN)
24853 return attribute;
24854
24855 {
24856 vec<tree, va_gc> *vec;
24857 int attr_flag = normal_attr;
24858
24859 if (attr_ns == get_identifier ("gnu")
24860 && attribute_takes_identifier_p (attr_id))
24861 /* A GNU attribute that takes an identifier in parameter. */
24862 attr_flag = id_attr;
24863
24864 vec = cp_parser_parenthesized_expression_list
24865 (parser, attr_flag, /*cast_p=*/false,
24866 /*allow_expansion_p=*/true,
24867 /*non_constant_p=*/NULL);
24868 if (vec == NULL)
24869 arguments = error_mark_node;
24870 else
24871 {
24872 arguments = build_tree_list_vec (vec);
24873 release_tree_vector (vec);
24874 }
24875
24876 if (arguments == error_mark_node)
24877 attribute = error_mark_node;
24878 else
24879 TREE_VALUE (attribute) = arguments;
24880 }
24881
24882 return attribute;
24883}
24884
24885/* Check that the attribute ATTRIBUTE appears at most once in the
24886 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24887 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24888 isn't implemented yet in GCC. */
24889
24890static void
24891cp_parser_check_std_attribute (tree attributes, tree attribute)
24892{
24893 if (attributes)
24894 {
24895 tree name = get_attribute_name (attribute);
24896 if (is_attribute_p ("noreturn", name)
24897 && lookup_attribute ("noreturn", attributes))
24898 error ("attribute %<noreturn%> can appear at most once "
24899 "in an attribute-list");
24900 else if (is_attribute_p ("deprecated", name)
24901 && lookup_attribute ("deprecated", attributes))
24902 error ("attribute %<deprecated%> can appear at most once "
24903 "in an attribute-list");
24904 }
24905}
24906
24907/* Parse a list of standard C++-11 attributes.
24908
24909 attribute-list:
24910 attribute [opt]
24911 attribute-list , attribute[opt]
24912 attribute ...
24913 attribute-list , attribute ...
24914*/
24915
24916static tree
24917cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24918{
24919 tree attributes = NULL_TREE, attribute = NULL_TREE;
24920 cp_token *token = NULL;
24921
24922 while (true)
24923 {
24924 attribute = cp_parser_std_attribute (parser, attr_ns);
24925 if (attribute == error_mark_node)
24926 break;
24927 if (attribute != NULL_TREE)
24928 {
24929 cp_parser_check_std_attribute (attributes, attribute);
24930 TREE_CHAIN (attribute) = attributes;
24931 attributes = attribute;
24932 }
24933 token = cp_lexer_peek_token (parser->lexer);
24934 if (token->type == CPP_ELLIPSIS)
24935 {
24936 cp_lexer_consume_token (parser->lexer);
24937 if (attribute == NULL_TREE)
24938 error_at (token->location,
24939 "expected attribute before %<...%>");
24940 else
24941 {
24942 tree pack = make_pack_expansion (TREE_VALUE (attribute));
24943 if (pack == error_mark_node)
24944 return error_mark_node;
24945 TREE_VALUE (attribute) = pack;
24946 }
24947 token = cp_lexer_peek_token (parser->lexer);
24948 }
24949 if (token->type != CPP_COMMA)
24950 break;
24951 cp_lexer_consume_token (parser->lexer);
24952 }
24953 attributes = nreverse (attributes);
24954 return attributes;
24955}
24956
24957/* Parse a standard C++-11 attribute specifier.
24958
24959 attribute-specifier:
24960 [ [ attribute-using-prefix [opt] attribute-list ] ]
24961 alignment-specifier
24962
24963 attribute-using-prefix:
24964 using attribute-namespace :
24965
24966 alignment-specifier:
24967 alignas ( type-id ... [opt] )
24968 alignas ( alignment-expression ... [opt] ). */
24969
24970static tree
24971cp_parser_std_attribute_spec (cp_parser *parser)
24972{
24973 tree attributes = NULL_TREE;
24974 cp_token *token = cp_lexer_peek_token (parser->lexer);
24975
24976 if (token->type == CPP_OPEN_SQUARE
24977 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24978 {
24979 tree attr_ns = NULL_TREE;
24980
24981 cp_lexer_consume_token (parser->lexer);
24982 cp_lexer_consume_token (parser->lexer);
24983
24984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24985 {
24986 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24987 if (token->type == CPP_NAME)
24988 attr_ns = token->u.value;
24989 else if (token->type == CPP_KEYWORD)
24990 attr_ns = ridpointers[(int) token->keyword];
24991 else if (token->flags & NAMED_OP)
24992 attr_ns = get_identifier (cpp_type2name (token->type,
24993 token->flags));
24994 if (attr_ns
24995 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24996 {
24997 if (cxx_dialect < cxx1z
24998 && !in_system_header_at (input_location))
24999 pedwarn (input_location, 0,
25000 "attribute using prefix only available "
25001 "with -std=c++1z or -std=gnu++1z");
25002
25003 cp_lexer_consume_token (parser->lexer);
25004 cp_lexer_consume_token (parser->lexer);
25005 cp_lexer_consume_token (parser->lexer);
25006 }
25007 else
25008 attr_ns = NULL_TREE;
25009 }
25010
25011 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25012
25013 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25014 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25015 cp_parser_skip_to_end_of_statement (parser);
25016 else
25017 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25018 when we are sure that we have actually parsed them. */
25019 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25020 }
25021 else
25022 {
25023 tree alignas_expr;
25024
25025 /* Look for an alignment-specifier. */
25026
25027 token = cp_lexer_peek_token (parser->lexer);
25028
25029 if (token->type != CPP_KEYWORD
25030 || token->keyword != RID_ALIGNAS)
25031 return NULL_TREE;
25032
25033 cp_lexer_consume_token (parser->lexer);
25034 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25035
25036 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
25037 {
25038 cp_parser_error (parser, "expected %<(%>");
25039 return error_mark_node;
25040 }
25041
25042 cp_parser_parse_tentatively (parser);
25043 alignas_expr = cp_parser_type_id (parser);
25044
25045 if (!cp_parser_parse_definitely (parser))
25046 {
25047 alignas_expr = cp_parser_assignment_expression (parser);
25048 if (alignas_expr == error_mark_node)
25049 cp_parser_skip_to_end_of_statement (parser);
25050 if (alignas_expr == NULL_TREE
25051 || alignas_expr == error_mark_node)
25052 return alignas_expr;
25053 }
25054
25055 alignas_expr = cxx_alignas_expr (alignas_expr);
25056 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25057
25058 /* Handle alignas (pack...). */
25059 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25060 {
25061 cp_lexer_consume_token (parser->lexer);
25062 alignas_expr = make_pack_expansion (alignas_expr);
25063 }
25064
25065 /* Something went wrong, so don't build the attribute. */
25066 if (alignas_expr == error_mark_node)
25067 return error_mark_node;
25068
25069 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25070 {
25071 cp_parser_error (parser, "expected %<)%>");
25072 return error_mark_node;
25073 }
25074
25075 /* Build the C++-11 representation of an 'aligned'
25076 attribute. */
25077 attributes =
25078 build_tree_list (build_tree_list (get_identifier ("gnu"),
25079 get_identifier ("aligned")),
25080 alignas_expr);
25081 }
25082
25083 return attributes;
25084}
25085
25086/* Parse a standard C++-11 attribute-specifier-seq.
25087
25088 attribute-specifier-seq:
25089 attribute-specifier-seq [opt] attribute-specifier
25090 */
25091
25092static tree
25093cp_parser_std_attribute_spec_seq (cp_parser *parser)
25094{
25095 tree attr_specs = NULL_TREE;
25096 tree attr_last = NULL_TREE;
25097
25098 while (true)
25099 {
25100 tree attr_spec = cp_parser_std_attribute_spec (parser);
25101 if (attr_spec == NULL_TREE)
25102 break;
25103 if (attr_spec == error_mark_node)
25104 return error_mark_node;
25105
25106 if (attr_last)
25107 TREE_CHAIN (attr_last) = attr_spec;
25108 else
25109 attr_specs = attr_last = attr_spec;
25110 attr_last = tree_last (attr_last);
25111 }
25112
25113 return attr_specs;
25114}
25115
25116/* Parse an optional `__extension__' keyword. Returns TRUE if it is
25117 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25118 current value of the PEDANTIC flag, regardless of whether or not
25119 the `__extension__' keyword is present. The caller is responsible
25120 for restoring the value of the PEDANTIC flag. */
25121
25122static bool
25123cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25124{
25125 /* Save the old value of the PEDANTIC flag. */
25126 *saved_pedantic = pedantic;
25127
25128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25129 {
25130 /* Consume the `__extension__' token. */
25131 cp_lexer_consume_token (parser->lexer);
25132 /* We're not being pedantic while the `__extension__' keyword is
25133 in effect. */
25134 pedantic = 0;
25135
25136 return true;
25137 }
25138
25139 return false;
25140}
25141
25142/* Parse a label declaration.
25143
25144 label-declaration:
25145 __label__ label-declarator-seq ;
25146
25147 label-declarator-seq:
25148 identifier , label-declarator-seq
25149 identifier */
25150
25151static void
25152cp_parser_label_declaration (cp_parser* parser)
25153{
25154 /* Look for the `__label__' keyword. */
25155 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25156
25157 while (true)
25158 {
25159 tree identifier;
25160
25161 /* Look for an identifier. */
25162 identifier = cp_parser_identifier (parser);
25163 /* If we failed, stop. */
25164 if (identifier == error_mark_node)
25165 break;
25166 /* Declare it as a label. */
25167 finish_label_decl (identifier);
25168 /* If the next token is a `;', stop. */
25169 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25170 break;
25171 /* Look for the `,' separating the label declarations. */
25172 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25173 }
25174
25175 /* Look for the final `;'. */
25176 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25177}
25178
25179// -------------------------------------------------------------------------- //
25180// Requires Clause
25181
25182// Parse a requires clause.
25183//
25184// requires-clause:
25185// 'requires' logical-or-expression
25186//
25187// The required logical-or-expression must be a constant expression. Note
25188// that we don't check that the expression is constepxr here. We defer until
25189// we analyze constraints and then, we only check atomic constraints.
25190static tree
25191cp_parser_requires_clause (cp_parser *parser)
25192{
25193 // Parse the requires clause so that it is not automatically folded.
25194 ++processing_template_decl;
25195 tree expr = cp_parser_binary_expression (parser, false, false,
25196 PREC_NOT_OPERATOR, NULL);
25197 if (check_for_bare_parameter_packs (expr))
25198 expr = error_mark_node;
25199 --processing_template_decl;
25200 return expr;
25201}
25202
25203// Optionally parse a requires clause:
25204static tree
25205cp_parser_requires_clause_opt (cp_parser *parser)
25206{
25207 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25208 if (tok->keyword != RID_REQUIRES)
25209 {
25210 if (!flag_concepts && tok->type == CPP_NAME
25211 && tok->u.value == ridpointers[RID_REQUIRES])
25212 {
25213 error_at (cp_lexer_peek_token (parser->lexer)->location,
25214 "%<requires%> only available with -fconcepts");
25215 /* Parse and discard the requires-clause. */
25216 cp_lexer_consume_token (parser->lexer);
25217 cp_parser_requires_clause (parser);
25218 }
25219 return NULL_TREE;
25220 }
25221 cp_lexer_consume_token (parser->lexer);
25222 return cp_parser_requires_clause (parser);
25223}
25224
25225
25226/*---------------------------------------------------------------------------
25227 Requires expressions
25228---------------------------------------------------------------------------*/
25229
25230/* Parse a requires expression
25231
25232 requirement-expression:
25233 'requires' requirement-parameter-list [opt] requirement-body */
25234static tree
25235cp_parser_requires_expression (cp_parser *parser)
25236{
25237 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25238 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25239
25240 /* A requires-expression shall appear only within a concept
25241 definition or a requires-clause.
25242
25243 TODO: Implement this diagnostic correctly. */
25244 if (!processing_template_decl)
25245 {
25246 error_at (loc, "a requires expression cannot appear outside a template");
25247 cp_parser_skip_to_end_of_statement (parser);
25248 return error_mark_node;
25249 }
25250
25251 tree parms, reqs;
25252 {
25253 /* Local parameters are delared as variables within the scope
25254 of the expression. They are not visible past the end of
25255 the expression. Expressions within the requires-expression
25256 are unevaluated. */
25257 struct scope_sentinel
25258 {
25259 scope_sentinel ()
25260 {
25261 ++cp_unevaluated_operand;
25262 begin_scope (sk_block, NULL_TREE);
25263 }
25264
25265 ~scope_sentinel ()
25266 {
25267 pop_bindings_and_leave_scope ();
25268 --cp_unevaluated_operand;
25269 }
25270 } s;
25271
25272 /* Parse the optional parameter list. */
25273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25274 {
25275 parms = cp_parser_requirement_parameter_list (parser);
25276 if (parms == error_mark_node)
25277 return error_mark_node;
25278 }
25279 else
25280 parms = NULL_TREE;
25281
25282 /* Parse the requirement body. */
25283 reqs = cp_parser_requirement_body (parser);
25284 if (reqs == error_mark_node)
25285 return error_mark_node;
25286 }
25287
25288 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25289 the parm chain. */
25290 grokparms (parms, &parms);
25291 return finish_requires_expr (parms, reqs);
25292}
25293
25294/* Parse a parameterized requirement.
25295
25296 requirement-parameter-list:
25297 '(' parameter-declaration-clause ')' */
25298static tree
25299cp_parser_requirement_parameter_list (cp_parser *parser)
25300{
25301 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25302 return error_mark_node;
25303
25304 tree parms = cp_parser_parameter_declaration_clause (parser);
25305
25306 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25307 return error_mark_node;
25308
25309 return parms;
25310}
25311
25312/* Parse the body of a requirement.
25313
25314 requirement-body:
25315 '{' requirement-list '}' */
25316static tree
25317cp_parser_requirement_body (cp_parser *parser)
25318{
25319 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25320 return error_mark_node;
25321
25322 tree reqs = cp_parser_requirement_list (parser);
25323
25324 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25325 return error_mark_node;
25326
25327 return reqs;
25328}
25329
25330/* Parse a list of requirements.
25331
25332 requirement-list:
25333 requirement
25334 requirement-list ';' requirement[opt] */
25335static tree
25336cp_parser_requirement_list (cp_parser *parser)
25337{
25338 tree result = NULL_TREE;
25339 while (true)
25340 {
25341 tree req = cp_parser_requirement (parser);
25342 if (req == error_mark_node)
25343 return error_mark_node;
25344
25345 result = tree_cons (NULL_TREE, req, result);
25346
25347 /* If we see a semi-colon, consume it. */
25348 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25349 cp_lexer_consume_token (parser->lexer);
25350
25351 /* Stop processing at the end of the list. */
25352 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25353 break;
25354 }
25355
25356 /* Reverse the order of requirements so they are analyzed in
25357 declaration order. */
25358 return nreverse (result);
25359}
25360
25361/* Parse a syntactic requirement or type requirement.
25362
25363 requirement:
25364 simple-requirement
25365 compound-requirement
25366 type-requirement
25367 nested-requirement */
25368static tree
25369cp_parser_requirement (cp_parser *parser)
25370{
25371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25372 return cp_parser_compound_requirement (parser);
25373 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25374 return cp_parser_type_requirement (parser);
25375 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25376 return cp_parser_nested_requirement (parser);
25377 else
25378 return cp_parser_simple_requirement (parser);
25379}
25380
25381/* Parse a simple requirement.
25382
25383 simple-requirement:
25384 expression ';' */
25385static tree
25386cp_parser_simple_requirement (cp_parser *parser)
25387{
25388 tree expr = cp_parser_expression (parser, NULL, false, false);
25389 if (!expr || expr == error_mark_node)
25390 return error_mark_node;
25391
25392 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25393 return error_mark_node;
25394
25395 return finish_simple_requirement (expr);
25396}
25397
25398/* Parse a type requirement
25399
25400 type-requirement
25401 nested-name-specifier [opt] required-type-name ';'
25402
25403 required-type-name:
25404 type-name
25405 'template' [opt] simple-template-id */
25406static tree
25407cp_parser_type_requirement (cp_parser *parser)
25408{
25409 cp_lexer_consume_token (parser->lexer);
25410
25411 // Save the scope before parsing name specifiers.
25412 tree saved_scope = parser->scope;
25413 tree saved_object_scope = parser->object_scope;
25414 tree saved_qualifying_scope = parser->qualifying_scope;
25415 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25416 cp_parser_nested_name_specifier_opt (parser,
25417 /*typename_keyword_p=*/true,
25418 /*check_dependency_p=*/false,
25419 /*type_p=*/true,
25420 /*is_declaration=*/false);
25421
25422 tree type;
25423 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25424 {
25425 cp_lexer_consume_token (parser->lexer);
25426 type = cp_parser_template_id (parser,
25427 /*template_keyword_p=*/true,
25428 /*check_dependency=*/false,
25429 /*tag_type=*/none_type,
25430 /*is_declaration=*/false);
25431 type = make_typename_type (parser->scope, type, typename_type,
25432 /*complain=*/tf_error);
25433 }
25434 else
25435 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25436
25437 if (TREE_CODE (type) == TYPE_DECL)
25438 type = TREE_TYPE (type);
25439
25440 parser->scope = saved_scope;
25441 parser->object_scope = saved_object_scope;
25442 parser->qualifying_scope = saved_qualifying_scope;
25443
25444 if (type == error_mark_node)
25445 cp_parser_skip_to_end_of_statement (parser);
25446
25447 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25448 return error_mark_node;
25449 if (type == error_mark_node)
25450 return error_mark_node;
25451
25452 return finish_type_requirement (type);
25453}
25454
25455/* Parse a compound requirement
25456
25457 compound-requirement:
25458 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25459static tree
25460cp_parser_compound_requirement (cp_parser *parser)
25461{
25462 /* Parse an expression enclosed in '{ }'s. */
25463 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25464 return error_mark_node;
25465
25466 tree expr = cp_parser_expression (parser, NULL, false, false);
25467 if (!expr || expr == error_mark_node)
25468 return error_mark_node;
25469
25470 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25471 return error_mark_node;
25472
25473 /* Parse the optional noexcept. */
25474 bool noexcept_p = false;
25475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25476 {
25477 cp_lexer_consume_token (parser->lexer);
25478 noexcept_p = true;
25479 }
25480
25481 /* Parse the optional trailing return type. */
25482 tree type = NULL_TREE;
25483 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25484 {
25485 cp_lexer_consume_token (parser->lexer);
25486 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25487 parser->in_result_type_constraint_p = true;
25488 type = cp_parser_trailing_type_id (parser);
25489 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25490 if (type == error_mark_node)
25491 return error_mark_node;
25492 }
25493
25494 return finish_compound_requirement (expr, type, noexcept_p);
25495}
25496
25497/* Parse a nested requirement. This is the same as a requires clause.
25498
25499 nested-requirement:
25500 requires-clause */
25501static tree
25502cp_parser_nested_requirement (cp_parser *parser)
25503{
25504 cp_lexer_consume_token (parser->lexer);
25505 tree req = cp_parser_requires_clause (parser);
25506 if (req == error_mark_node)
25507 return error_mark_node;
25508 return finish_nested_requirement (req);
25509}
25510
25511/* Support Functions */
25512
25513/* Return the appropriate prefer_type argument for lookup_name_real based on
25514 tag_type and template_mem_access. */
25515
25516static inline int
25517prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25518{
25519 /* DR 141: When looking in the current enclosing context for a template-name
25520 after -> or ., only consider class templates. */
25521 if (template_mem_access)
25522 return 2;
25523 switch (tag_type)
25524 {
25525 case none_type: return 0; // No preference.
25526 case scope_type: return 1; // Type or namespace.
25527 default: return 2; // Type only.
25528 }
25529}
25530
25531/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25532 NAME should have one of the representations used for an
25533 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25534 is returned. If PARSER->SCOPE is a dependent type, then a
25535 SCOPE_REF is returned.
25536
25537 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25538 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25539 was formed. Abstractly, such entities should not be passed to this
25540 function, because they do not need to be looked up, but it is
25541 simpler to check for this special case here, rather than at the
25542 call-sites.
25543
25544 In cases not explicitly covered above, this function returns a
25545 DECL, OVERLOAD, or baselink representing the result of the lookup.
25546 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25547 is returned.
25548
25549 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25550 (e.g., "struct") that was used. In that case bindings that do not
25551 refer to types are ignored.
25552
25553 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25554 ignored.
25555
25556 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25557 are ignored.
25558
25559 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25560 types.
25561
25562 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25563 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25564 NULL_TREE otherwise. */
25565
25566static cp_expr
25567cp_parser_lookup_name (cp_parser *parser, tree name,
25568 enum tag_types tag_type,
25569 bool is_template,
25570 bool is_namespace,
25571 bool check_dependency,
25572 tree *ambiguous_decls,
25573 location_t name_location)
25574{
25575 tree decl;
25576 tree object_type = parser->context->object_type;
25577
25578 /* Assume that the lookup will be unambiguous. */
25579 if (ambiguous_decls)
25580 *ambiguous_decls = NULL_TREE;
25581
25582 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25583 no longer valid. Note that if we are parsing tentatively, and
25584 the parse fails, OBJECT_TYPE will be automatically restored. */
25585 parser->context->object_type = NULL_TREE;
25586
25587 if (name == error_mark_node)
25588 return error_mark_node;
25589
25590 /* A template-id has already been resolved; there is no lookup to
25591 do. */
25592 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25593 return name;
25594 if (BASELINK_P (name))
25595 {
25596 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25597 == TEMPLATE_ID_EXPR);
25598 return name;
25599 }
25600
25601 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25602 it should already have been checked to make sure that the name
25603 used matches the type being destroyed. */
25604 if (TREE_CODE (name) == BIT_NOT_EXPR)
25605 {
25606 tree type;
25607
25608 /* Figure out to which type this destructor applies. */
25609 if (parser->scope)
25610 type = parser->scope;
25611 else if (object_type)
25612 type = object_type;
25613 else
25614 type = current_class_type;
25615 /* If that's not a class type, there is no destructor. */
25616 if (!type || !CLASS_TYPE_P (type))
25617 return error_mark_node;
25618 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25619 lazily_declare_fn (sfk_destructor, type);
25620 if (!CLASSTYPE_DESTRUCTORS (type))
25621 return error_mark_node;
25622 /* If it was a class type, return the destructor. */
25623 return CLASSTYPE_DESTRUCTORS (type);
25624 }
25625
25626 /* By this point, the NAME should be an ordinary identifier. If
25627 the id-expression was a qualified name, the qualifying scope is
25628 stored in PARSER->SCOPE at this point. */
25629 gcc_assert (identifier_p (name));
25630
25631 /* Perform the lookup. */
25632 if (parser->scope)
25633 {
25634 bool dependent_p;
25635
25636 if (parser->scope == error_mark_node)
25637 return error_mark_node;
25638
25639 /* If the SCOPE is dependent, the lookup must be deferred until
25640 the template is instantiated -- unless we are explicitly
25641 looking up names in uninstantiated templates. Even then, we
25642 cannot look up the name if the scope is not a class type; it
25643 might, for example, be a template type parameter. */
25644 dependent_p = (TYPE_P (parser->scope)
25645 && dependent_scope_p (parser->scope));
25646 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25647 && dependent_p)
25648 /* Defer lookup. */
25649 decl = error_mark_node;
25650 else
25651 {
25652 tree pushed_scope = NULL_TREE;
25653
25654 /* If PARSER->SCOPE is a dependent type, then it must be a
25655 class type, and we must not be checking dependencies;
25656 otherwise, we would have processed this lookup above. So
25657 that PARSER->SCOPE is not considered a dependent base by
25658 lookup_member, we must enter the scope here. */
25659 if (dependent_p)
25660 pushed_scope = push_scope (parser->scope);
25661
25662 /* If the PARSER->SCOPE is a template specialization, it
25663 may be instantiated during name lookup. In that case,
25664 errors may be issued. Even if we rollback the current
25665 tentative parse, those errors are valid. */
25666 decl = lookup_qualified_name (parser->scope, name,
25667 prefer_type_arg (tag_type),
25668 /*complain=*/true);
25669
25670 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25671 lookup result and the nested-name-specifier nominates a class C:
25672 * if the name specified after the nested-name-specifier, when
25673 looked up in C, is the injected-class-name of C (Clause 9), or
25674 * if the name specified after the nested-name-specifier is the
25675 same as the identifier or the simple-template-id's template-
25676 name in the last component of the nested-name-specifier,
25677 the name is instead considered to name the constructor of
25678 class C. [ Note: for example, the constructor is not an
25679 acceptable lookup result in an elaborated-type-specifier so
25680 the constructor would not be used in place of the
25681 injected-class-name. --end note ] Such a constructor name
25682 shall be used only in the declarator-id of a declaration that
25683 names a constructor or in a using-declaration. */
25684 if (tag_type == none_type
25685 && DECL_SELF_REFERENCE_P (decl)
25686 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25687 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25688 prefer_type_arg (tag_type),
25689 /*complain=*/true);
25690
25691 /* If we have a single function from a using decl, pull it out. */
25692 if (TREE_CODE (decl) == OVERLOAD
25693 && !really_overloaded_fn (decl))
25694 decl = OVL_FUNCTION (decl);
25695
25696 if (pushed_scope)
25697 pop_scope (pushed_scope);
25698 }
25699
25700 /* If the scope is a dependent type and either we deferred lookup or
25701 we did lookup but didn't find the name, rememeber the name. */
25702 if (decl == error_mark_node && TYPE_P (parser->scope)
25703 && dependent_type_p (parser->scope))
25704 {
25705 if (tag_type)
25706 {
25707 tree type;
25708
25709 /* The resolution to Core Issue 180 says that `struct
25710 A::B' should be considered a type-name, even if `A'
25711 is dependent. */
25712 type = make_typename_type (parser->scope, name, tag_type,
25713 /*complain=*/tf_error);
25714 if (type != error_mark_node)
25715 decl = TYPE_NAME (type);
25716 }
25717 else if (is_template
25718 && (cp_parser_next_token_ends_template_argument_p (parser)
25719 || cp_lexer_next_token_is (parser->lexer,
25720 CPP_CLOSE_PAREN)))
25721 decl = make_unbound_class_template (parser->scope,
25722 name, NULL_TREE,
25723 /*complain=*/tf_error);
25724 else
25725 decl = build_qualified_name (/*type=*/NULL_TREE,
25726 parser->scope, name,
25727 is_template);
25728 }
25729 parser->qualifying_scope = parser->scope;
25730 parser->object_scope = NULL_TREE;
25731 }
25732 else if (object_type)
25733 {
25734 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25735 OBJECT_TYPE is not a class. */
25736 if (CLASS_TYPE_P (object_type))
25737 /* If the OBJECT_TYPE is a template specialization, it may
25738 be instantiated during name lookup. In that case, errors
25739 may be issued. Even if we rollback the current tentative
25740 parse, those errors are valid. */
25741 decl = lookup_member (object_type,
25742 name,
25743 /*protect=*/0,
25744 prefer_type_arg (tag_type),
25745 tf_warning_or_error);
25746 else
25747 decl = NULL_TREE;
25748
25749 if (!decl)
25750 /* Look it up in the enclosing context. DR 141: When looking for a
25751 template-name after -> or ., only consider class templates. */
25752 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25753 /*nonclass=*/0,
25754 /*block_p=*/true, is_namespace, 0);
25755 if (object_type == unknown_type_node)
25756 /* The object is type-dependent, so we can't look anything up; we used
25757 this to get the DR 141 behavior. */
25758 object_type = NULL_TREE;
25759 parser->object_scope = object_type;
25760 parser->qualifying_scope = NULL_TREE;
25761 }
25762 else
25763 {
25764 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25765 /*nonclass=*/0,
25766 /*block_p=*/true, is_namespace, 0);
25767 parser->qualifying_scope = NULL_TREE;
25768 parser->object_scope = NULL_TREE;
25769 }
25770
25771 /* If the lookup failed, let our caller know. */
25772 if (!decl || decl == error_mark_node)
25773 return error_mark_node;
25774
25775 /* Pull out the template from an injected-class-name (or multiple). */
25776 if (is_template)
25777 decl = maybe_get_template_decl_from_type_decl (decl);
25778
25779 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25780 if (TREE_CODE (decl) == TREE_LIST)
25781 {
25782 if (ambiguous_decls)
25783 *ambiguous_decls = decl;
25784 /* The error message we have to print is too complicated for
25785 cp_parser_error, so we incorporate its actions directly. */
25786 if (!cp_parser_simulate_error (parser))
25787 {
25788 error_at (name_location, "reference to %qD is ambiguous",
25789 name);
25790 print_candidates (decl);
25791 }
25792 return error_mark_node;
25793 }
25794
25795 gcc_assert (DECL_P (decl)
25796 || TREE_CODE (decl) == OVERLOAD
25797 || TREE_CODE (decl) == SCOPE_REF
25798 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25799 || BASELINK_P (decl));
25800
25801 /* If we have resolved the name of a member declaration, check to
25802 see if the declaration is accessible. When the name resolves to
25803 set of overloaded functions, accessibility is checked when
25804 overload resolution is done.
25805
25806 During an explicit instantiation, access is not checked at all,
25807 as per [temp.explicit]. */
25808 if (DECL_P (decl))
25809 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25810
25811 maybe_record_typedef_use (decl);
25812
25813 return cp_expr (decl, name_location);
25814}
25815
25816/* Like cp_parser_lookup_name, but for use in the typical case where
25817 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25818 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25819
25820static tree
25821cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25822{
25823 return cp_parser_lookup_name (parser, name,
25824 none_type,
25825 /*is_template=*/false,
25826 /*is_namespace=*/false,
25827 /*check_dependency=*/true,
25828 /*ambiguous_decls=*/NULL,
25829 location);
25830}
25831
25832/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25833 the current context, return the TYPE_DECL. If TAG_NAME_P is
25834 true, the DECL indicates the class being defined in a class-head,
25835 or declared in an elaborated-type-specifier.
25836
25837 Otherwise, return DECL. */
25838
25839static tree
25840cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25841{
25842 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25843 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25844
25845 struct A {
25846 template <typename T> struct B;
25847 };
25848
25849 template <typename T> struct A::B {};
25850
25851 Similarly, in an elaborated-type-specifier:
25852
25853 namespace N { struct X{}; }
25854
25855 struct A {
25856 template <typename T> friend struct N::X;
25857 };
25858
25859 However, if the DECL refers to a class type, and we are in
25860 the scope of the class, then the name lookup automatically
25861 finds the TYPE_DECL created by build_self_reference rather
25862 than a TEMPLATE_DECL. For example, in:
25863
25864 template <class T> struct S {
25865 S s;
25866 };
25867
25868 there is no need to handle such case. */
25869
25870 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25871 return DECL_TEMPLATE_RESULT (decl);
25872
25873 return decl;
25874}
25875
25876/* If too many, or too few, template-parameter lists apply to the
25877 declarator, issue an error message. Returns TRUE if all went well,
25878 and FALSE otherwise. */
25879
25880static bool
25881cp_parser_check_declarator_template_parameters (cp_parser* parser,
25882 cp_declarator *declarator,
25883 location_t declarator_location)
25884{
25885 switch (declarator->kind)
25886 {
25887 case cdk_id:
25888 {
25889 unsigned num_templates = 0;
25890 tree scope = declarator->u.id.qualifying_scope;
25891
25892 if (scope)
25893 num_templates = num_template_headers_for_class (scope);
25894 else if (TREE_CODE (declarator->u.id.unqualified_name)
25895 == TEMPLATE_ID_EXPR)
25896 /* If the DECLARATOR has the form `X<y>' then it uses one
25897 additional level of template parameters. */
25898 ++num_templates;
25899
25900 return cp_parser_check_template_parameters
25901 (parser, num_templates, declarator_location, declarator);
25902 }
25903
25904 case cdk_function:
25905 case cdk_array:
25906 case cdk_pointer:
25907 case cdk_reference:
25908 case cdk_ptrmem:
25909 return (cp_parser_check_declarator_template_parameters
25910 (parser, declarator->declarator, declarator_location));
25911
25912 case cdk_decomp:
25913 case cdk_error:
25914 return true;
25915
25916 default:
25917 gcc_unreachable ();
25918 }
25919 return false;
25920}
25921
25922/* NUM_TEMPLATES were used in the current declaration. If that is
25923 invalid, return FALSE and issue an error messages. Otherwise,
25924 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25925 declarator and we can print more accurate diagnostics. */
25926
25927static bool
25928cp_parser_check_template_parameters (cp_parser* parser,
25929 unsigned num_templates,
25930 location_t location,
25931 cp_declarator *declarator)
25932{
25933 /* If there are the same number of template classes and parameter
25934 lists, that's OK. */
25935 if (parser->num_template_parameter_lists == num_templates)
25936 return true;
25937 /* If there are more, but only one more, then we are referring to a
25938 member template. That's OK too. */
25939 if (parser->num_template_parameter_lists == num_templates + 1)
25940 return true;
25941 /* If there are more template classes than parameter lists, we have
25942 something like:
25943
25944 template <class T> void S<T>::R<T>::f (); */
25945 if (parser->num_template_parameter_lists < num_templates)
25946 {
25947 if (declarator && !current_function_decl)
25948 error_at (location, "specializing member %<%T::%E%> "
25949 "requires %<template<>%> syntax",
25950 declarator->u.id.qualifying_scope,
25951 declarator->u.id.unqualified_name);
25952 else if (declarator)
25953 error_at (location, "invalid declaration of %<%T::%E%>",
25954 declarator->u.id.qualifying_scope,
25955 declarator->u.id.unqualified_name);
25956 else
25957 error_at (location, "too few template-parameter-lists");
25958 return false;
25959 }
25960 /* Otherwise, there are too many template parameter lists. We have
25961 something like:
25962
25963 template <class T> template <class U> void S::f(); */
25964 error_at (location, "too many template-parameter-lists");
25965 return false;
25966}
25967
25968/* Parse an optional `::' token indicating that the following name is
25969 from the global namespace. If so, PARSER->SCOPE is set to the
25970 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25971 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25972 Returns the new value of PARSER->SCOPE, if the `::' token is
25973 present, and NULL_TREE otherwise. */
25974
25975static tree
25976cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25977{
25978 cp_token *token;
25979
25980 /* Peek at the next token. */
25981 token = cp_lexer_peek_token (parser->lexer);
25982 /* If we're looking at a `::' token then we're starting from the
25983 global namespace, not our current location. */
25984 if (token->type == CPP_SCOPE)
25985 {
25986 /* Consume the `::' token. */
25987 cp_lexer_consume_token (parser->lexer);
25988 /* Set the SCOPE so that we know where to start the lookup. */
25989 parser->scope = global_namespace;
25990 parser->qualifying_scope = global_namespace;
25991 parser->object_scope = NULL_TREE;
25992
25993 return parser->scope;
25994 }
25995 else if (!current_scope_valid_p)
25996 {
25997 parser->scope = NULL_TREE;
25998 parser->qualifying_scope = NULL_TREE;
25999 parser->object_scope = NULL_TREE;
26000 }
26001
26002 return NULL_TREE;
26003}
26004
26005/* Returns TRUE if the upcoming token sequence is the start of a
26006 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26007 declarator is preceded by the `friend' specifier. */
26008
26009static bool
26010cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26011{
26012 bool constructor_p;
26013 bool outside_class_specifier_p;
26014 tree nested_name_specifier;
26015 cp_token *next_token;
26016
26017 /* The common case is that this is not a constructor declarator, so
26018 try to avoid doing lots of work if at all possible. It's not
26019 valid declare a constructor at function scope. */
26020 if (parser->in_function_body)
26021 return false;
26022 /* And only certain tokens can begin a constructor declarator. */
26023 next_token = cp_lexer_peek_token (parser->lexer);
26024 if (next_token->type != CPP_NAME
26025 && next_token->type != CPP_SCOPE
26026 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26027 && next_token->type != CPP_TEMPLATE_ID)
26028 return false;
26029
26030 /* Parse tentatively; we are going to roll back all of the tokens
26031 consumed here. */
26032 cp_parser_parse_tentatively (parser);
26033 /* Assume that we are looking at a constructor declarator. */
26034 constructor_p = true;
26035
26036 /* Look for the optional `::' operator. */
26037 cp_parser_global_scope_opt (parser,
26038 /*current_scope_valid_p=*/false);
26039 /* Look for the nested-name-specifier. */
26040 nested_name_specifier
26041 = (cp_parser_nested_name_specifier_opt (parser,
26042 /*typename_keyword_p=*/false,
26043 /*check_dependency_p=*/false,
26044 /*type_p=*/false,
26045 /*is_declaration=*/false));
26046
26047 outside_class_specifier_p = (!at_class_scope_p ()
26048 || !TYPE_BEING_DEFINED (current_class_type)
26049 || friend_p);
26050
26051 /* Outside of a class-specifier, there must be a
26052 nested-name-specifier. Except in C++17 mode, where we
26053 might be declaring a guiding declaration. */
26054 if (!nested_name_specifier && outside_class_specifier_p
26055 && cxx_dialect < cxx1z)
26056 constructor_p = false;
26057 else if (nested_name_specifier == error_mark_node)
26058 constructor_p = false;
26059
26060 /* If we have a class scope, this is easy; DR 147 says that S::S always
26061 names the constructor, and no other qualified name could. */
26062 if (constructor_p && nested_name_specifier
26063 && CLASS_TYPE_P (nested_name_specifier))
26064 {
26065 tree id = cp_parser_unqualified_id (parser,
26066 /*template_keyword_p=*/false,
26067 /*check_dependency_p=*/false,
26068 /*declarator_p=*/true,
26069 /*optional_p=*/false);
26070 if (is_overloaded_fn (id))
26071 id = DECL_NAME (get_first_fn (id));
26072 if (!constructor_name_p (id, nested_name_specifier))
26073 constructor_p = false;
26074 }
26075 /* If we still think that this might be a constructor-declarator,
26076 look for a class-name. */
26077 else if (constructor_p)
26078 {
26079 /* If we have:
26080
26081 template <typename T> struct S {
26082 S();
26083 };
26084
26085 we must recognize that the nested `S' names a class. */
26086 if (cxx_dialect >= cxx1z)
26087 cp_parser_parse_tentatively (parser);
26088
26089 tree type_decl;
26090 type_decl = cp_parser_class_name (parser,
26091 /*typename_keyword_p=*/false,
26092 /*template_keyword_p=*/false,
26093 none_type,
26094 /*check_dependency_p=*/false,
26095 /*class_head_p=*/false,
26096 /*is_declaration=*/false);
26097
26098 if (cxx_dialect >= cxx1z
26099 && !cp_parser_parse_definitely (parser))
26100 {
26101 type_decl = NULL_TREE;
26102 tree tmpl = cp_parser_template_name (parser,
26103 /*template_keyword*/false,
26104 /*check_dependency_p*/false,
26105 /*is_declaration*/false,
26106 none_type,
26107 /*is_identifier*/NULL);
26108 if (DECL_CLASS_TEMPLATE_P (tmpl)
26109 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26110 /* It's a deduction guide, return true. */;
26111 else
26112 cp_parser_simulate_error (parser);
26113 }
26114
26115 /* If there was no class-name, then this is not a constructor.
26116 Otherwise, if we are in a class-specifier and we aren't
26117 handling a friend declaration, check that its type matches
26118 current_class_type (c++/38313). Note: error_mark_node
26119 is left alone for error recovery purposes. */
26120 constructor_p = (!cp_parser_error_occurred (parser)
26121 && (outside_class_specifier_p
26122 || type_decl == NULL_TREE
26123 || type_decl == error_mark_node
26124 || same_type_p (current_class_type,
26125 TREE_TYPE (type_decl))));
26126
26127 /* If we're still considering a constructor, we have to see a `(',
26128 to begin the parameter-declaration-clause, followed by either a
26129 `)', an `...', or a decl-specifier. We need to check for a
26130 type-specifier to avoid being fooled into thinking that:
26131
26132 S (f) (int);
26133
26134 is a constructor. (It is actually a function named `f' that
26135 takes one parameter (of type `int') and returns a value of type
26136 `S'. */
26137 if (constructor_p
26138 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26139 constructor_p = false;
26140
26141 if (constructor_p
26142 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26143 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26144 /* A parameter declaration begins with a decl-specifier,
26145 which is either the "attribute" keyword, a storage class
26146 specifier, or (usually) a type-specifier. */
26147 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26148 {
26149 tree type;
26150 tree pushed_scope = NULL_TREE;
26151 unsigned saved_num_template_parameter_lists;
26152
26153 /* Names appearing in the type-specifier should be looked up
26154 in the scope of the class. */
26155 if (current_class_type)
26156 type = NULL_TREE;
26157 else if (type_decl)
26158 {
26159 type = TREE_TYPE (type_decl);
26160 if (TREE_CODE (type) == TYPENAME_TYPE)
26161 {
26162 type = resolve_typename_type (type,
26163 /*only_current_p=*/false);
26164 if (TREE_CODE (type) == TYPENAME_TYPE)
26165 {
26166 cp_parser_abort_tentative_parse (parser);
26167 return false;
26168 }
26169 }
26170 pushed_scope = push_scope (type);
26171 }
26172
26173 /* Inside the constructor parameter list, surrounding
26174 template-parameter-lists do not apply. */
26175 saved_num_template_parameter_lists
26176 = parser->num_template_parameter_lists;
26177 parser->num_template_parameter_lists = 0;
26178
26179 /* Look for the type-specifier. */
26180 cp_parser_type_specifier (parser,
26181 CP_PARSER_FLAGS_NONE,
26182 /*decl_specs=*/NULL,
26183 /*is_declarator=*/true,
26184 /*declares_class_or_enum=*/NULL,
26185 /*is_cv_qualifier=*/NULL);
26186
26187 parser->num_template_parameter_lists
26188 = saved_num_template_parameter_lists;
26189
26190 /* Leave the scope of the class. */
26191 if (pushed_scope)
26192 pop_scope (pushed_scope);
26193
26194 constructor_p = !cp_parser_error_occurred (parser);
26195 }
26196 }
26197
26198 /* We did not really want to consume any tokens. */
26199 cp_parser_abort_tentative_parse (parser);
26200
26201 return constructor_p;
26202}
26203
26204/* Parse the definition of the function given by the DECL_SPECIFIERS,
26205 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26206 they must be performed once we are in the scope of the function.
26207
26208 Returns the function defined. */
26209
26210static tree
26211cp_parser_function_definition_from_specifiers_and_declarator
26212 (cp_parser* parser,
26213 cp_decl_specifier_seq *decl_specifiers,
26214 tree attributes,
26215 const cp_declarator *declarator)
26216{
26217 tree fn;
26218 bool success_p;
26219
26220 /* Begin the function-definition. */
26221 success_p = start_function (decl_specifiers, declarator, attributes);
26222
26223 /* The things we're about to see are not directly qualified by any
26224 template headers we've seen thus far. */
26225 reset_specialization ();
26226
26227 /* If there were names looked up in the decl-specifier-seq that we
26228 did not check, check them now. We must wait until we are in the
26229 scope of the function to perform the checks, since the function
26230 might be a friend. */
26231 perform_deferred_access_checks (tf_warning_or_error);
26232
26233 if (success_p)
26234 {
26235 cp_finalize_omp_declare_simd (parser, current_function_decl);
26236 parser->omp_declare_simd = NULL;
26237 cp_finalize_oacc_routine (parser, current_function_decl, true);
26238 parser->oacc_routine = NULL;
26239 }
26240
26241 if (!success_p)
26242 {
26243 /* Skip the entire function. */
26244 cp_parser_skip_to_end_of_block_or_statement (parser);
26245 fn = error_mark_node;
26246 }
26247 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26248 {
26249 /* Seen already, skip it. An error message has already been output. */
26250 cp_parser_skip_to_end_of_block_or_statement (parser);
26251 fn = current_function_decl;
26252 current_function_decl = NULL_TREE;
26253 /* If this is a function from a class, pop the nested class. */
26254 if (current_class_name)
26255 pop_nested_class ();
26256 }
26257 else
26258 {
26259 timevar_id_t tv;
26260 if (DECL_DECLARED_INLINE_P (current_function_decl))
26261 tv = TV_PARSE_INLINE;
26262 else
26263 tv = TV_PARSE_FUNC;
26264 timevar_push (tv);
26265 fn = cp_parser_function_definition_after_declarator (parser,
26266 /*inline_p=*/false);
26267 timevar_pop (tv);
26268 }
26269
26270 return fn;
26271}
26272
26273/* Parse the part of a function-definition that follows the
26274 declarator. INLINE_P is TRUE iff this function is an inline
26275 function defined within a class-specifier.
26276
26277 Returns the function defined. */
26278
26279static tree
26280cp_parser_function_definition_after_declarator (cp_parser* parser,
26281 bool inline_p)
26282{
26283 tree fn;
26284 bool ctor_initializer_p = false;
26285 bool saved_in_unbraced_linkage_specification_p;
26286 bool saved_in_function_body;
26287 unsigned saved_num_template_parameter_lists;
26288 cp_token *token;
26289 bool fully_implicit_function_template_p
26290 = parser->fully_implicit_function_template_p;
26291 parser->fully_implicit_function_template_p = false;
26292 tree implicit_template_parms
26293 = parser->implicit_template_parms;
26294 parser->implicit_template_parms = 0;
26295 cp_binding_level* implicit_template_scope
26296 = parser->implicit_template_scope;
26297 parser->implicit_template_scope = 0;
26298
26299 saved_in_function_body = parser->in_function_body;
26300 parser->in_function_body = true;
26301 /* If the next token is `return', then the code may be trying to
26302 make use of the "named return value" extension that G++ used to
26303 support. */
26304 token = cp_lexer_peek_token (parser->lexer);
26305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26306 {
26307 /* Consume the `return' keyword. */
26308 cp_lexer_consume_token (parser->lexer);
26309 /* Look for the identifier that indicates what value is to be
26310 returned. */
26311 cp_parser_identifier (parser);
26312 /* Issue an error message. */
26313 error_at (token->location,
26314 "named return values are no longer supported");
26315 /* Skip tokens until we reach the start of the function body. */
26316 while (true)
26317 {
26318 cp_token *token = cp_lexer_peek_token (parser->lexer);
26319 if (token->type == CPP_OPEN_BRACE
26320 || token->type == CPP_EOF
26321 || token->type == CPP_PRAGMA_EOL)
26322 break;
26323 cp_lexer_consume_token (parser->lexer);
26324 }
26325 }
26326 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26327 anything declared inside `f'. */
26328 saved_in_unbraced_linkage_specification_p
26329 = parser->in_unbraced_linkage_specification_p;
26330 parser->in_unbraced_linkage_specification_p = false;
26331 /* Inside the function, surrounding template-parameter-lists do not
26332 apply. */
26333 saved_num_template_parameter_lists
26334 = parser->num_template_parameter_lists;
26335 parser->num_template_parameter_lists = 0;
26336
26337 start_lambda_scope (current_function_decl);
26338
26339 /* If the next token is `try', `__transaction_atomic', or
26340 `__transaction_relaxed`, then we are looking at either function-try-block
26341 or function-transaction-block. Note that all of these include the
26342 function-body. */
26343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26344 ctor_initializer_p = cp_parser_function_transaction (parser,
26345 RID_TRANSACTION_ATOMIC);
26346 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26347 RID_TRANSACTION_RELAXED))
26348 ctor_initializer_p = cp_parser_function_transaction (parser,
26349 RID_TRANSACTION_RELAXED);
26350 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26351 ctor_initializer_p = cp_parser_function_try_block (parser);
26352 else
26353 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26354 (parser, /*in_function_try_block=*/false);
26355
26356 finish_lambda_scope ();
26357
26358 /* Finish the function. */
26359 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26360 (inline_p ? 2 : 0));
26361 /* Generate code for it, if necessary. */
26362 expand_or_defer_fn (fn);
26363 /* Restore the saved values. */
26364 parser->in_unbraced_linkage_specification_p
26365 = saved_in_unbraced_linkage_specification_p;
26366 parser->num_template_parameter_lists
26367 = saved_num_template_parameter_lists;
26368 parser->in_function_body = saved_in_function_body;
26369
26370 parser->fully_implicit_function_template_p
26371 = fully_implicit_function_template_p;
26372 parser->implicit_template_parms
26373 = implicit_template_parms;
26374 parser->implicit_template_scope
26375 = implicit_template_scope;
26376
26377 if (parser->fully_implicit_function_template_p)
26378 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26379
26380 return fn;
26381}
26382
26383/* Parse a template-declaration body (following argument list). */
26384
26385static void
26386cp_parser_template_declaration_after_parameters (cp_parser* parser,
26387 tree parameter_list,
26388 bool member_p)
26389{
26390 tree decl = NULL_TREE;
26391 bool friend_p = false;
26392
26393 /* We just processed one more parameter list. */
26394 ++parser->num_template_parameter_lists;
26395
26396 /* Get the deferred access checks from the parameter list. These
26397 will be checked once we know what is being declared, as for a
26398 member template the checks must be performed in the scope of the
26399 class containing the member. */
26400 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26401
26402 /* Tentatively parse for a new template parameter list, which can either be
26403 the template keyword or a template introduction. */
26404 if (cp_parser_template_declaration_after_export (parser, member_p))
26405 /* OK */;
26406 else if (cxx_dialect >= cxx11
26407 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26408 decl = cp_parser_alias_declaration (parser);
26409 else
26410 {
26411 /* There are no access checks when parsing a template, as we do not
26412 know if a specialization will be a friend. */
26413 push_deferring_access_checks (dk_no_check);
26414 cp_token *token = cp_lexer_peek_token (parser->lexer);
26415 decl = cp_parser_single_declaration (parser,
26416 checks,
26417 member_p,
26418 /*explicit_specialization_p=*/false,
26419 &friend_p);
26420 pop_deferring_access_checks ();
26421
26422 /* If this is a member template declaration, let the front
26423 end know. */
26424 if (member_p && !friend_p && decl)
26425 {
26426 if (TREE_CODE (decl) == TYPE_DECL)
26427 cp_parser_check_access_in_redeclaration (decl, token->location);
26428
26429 decl = finish_member_template_decl (decl);
26430 }
26431 else if (friend_p && decl
26432 && DECL_DECLARES_TYPE_P (decl))
26433 make_friend_class (current_class_type, TREE_TYPE (decl),
26434 /*complain=*/true);
26435 }
26436 /* We are done with the current parameter list. */
26437 --parser->num_template_parameter_lists;
26438
26439 pop_deferring_access_checks ();
26440
26441 /* Finish up. */
26442 finish_template_decl (parameter_list);
26443
26444 /* Check the template arguments for a literal operator template. */
26445 if (decl
26446 && DECL_DECLARES_FUNCTION_P (decl)
26447 && UDLIT_OPER_P (DECL_NAME (decl)))
26448 {
26449 bool ok = true;
26450 if (parameter_list == NULL_TREE)
26451 ok = false;
26452 else
26453 {
26454 int num_parms = TREE_VEC_LENGTH (parameter_list);
26455 if (num_parms == 1)
26456 {
26457 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26458 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26459 if (TREE_TYPE (parm) != char_type_node
26460 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26461 ok = false;
26462 }
26463 else if (num_parms == 2 && cxx_dialect >= cxx14)
26464 {
26465 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26466 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26467 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26468 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26469 if (parm == error_mark_node
26470 || TREE_TYPE (parm) != TREE_TYPE (type)
26471 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26472 ok = false;
26473 }
26474 else
26475 ok = false;
26476 }
26477 if (!ok)
26478 {
26479 if (cxx_dialect >= cxx14)
26480 error ("literal operator template %qD has invalid parameter list."
26481 " Expected non-type template argument pack <char...>"
26482 " or <typename CharT, CharT...>",
26483 decl);
26484 else
26485 error ("literal operator template %qD has invalid parameter list."
26486 " Expected non-type template argument pack <char...>",
26487 decl);
26488 }
26489 }
26490
26491 /* Register member declarations. */
26492 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26493 finish_member_declaration (decl);
26494 /* If DECL is a function template, we must return to parse it later.
26495 (Even though there is no definition, there might be default
26496 arguments that need handling.) */
26497 if (member_p && decl
26498 && DECL_DECLARES_FUNCTION_P (decl))
26499 vec_safe_push (unparsed_funs_with_definitions, decl);
26500}
26501
26502/* Parse a template introduction header for a template-declaration. Returns
26503 false if tentative parse fails. */
26504
26505static bool
26506cp_parser_template_introduction (cp_parser* parser, bool member_p)
26507{
26508 cp_parser_parse_tentatively (parser);
26509
26510 tree saved_scope = parser->scope;
26511 tree saved_object_scope = parser->object_scope;
26512 tree saved_qualifying_scope = parser->qualifying_scope;
26513
26514 /* Look for the optional `::' operator. */
26515 cp_parser_global_scope_opt (parser,
26516 /*current_scope_valid_p=*/false);
26517 /* Look for the nested-name-specifier. */
26518 cp_parser_nested_name_specifier_opt (parser,
26519 /*typename_keyword_p=*/false,
26520 /*check_dependency_p=*/true,
26521 /*type_p=*/false,
26522 /*is_declaration=*/false);
26523
26524 cp_token *token = cp_lexer_peek_token (parser->lexer);
26525 tree concept_name = cp_parser_identifier (parser);
26526
26527 /* Look up the concept for which we will be matching
26528 template parameters. */
26529 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26530 token->location);
26531 parser->scope = saved_scope;
26532 parser->object_scope = saved_object_scope;
26533 parser->qualifying_scope = saved_qualifying_scope;
26534
26535 if (concept_name == error_mark_node)
26536 cp_parser_simulate_error (parser);
26537
26538 /* Look for opening brace for introduction. */
26539 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26540
26541 if (!cp_parser_parse_definitely (parser))
26542 return false;
26543
26544 push_deferring_access_checks (dk_deferred);
26545
26546 /* Build vector of placeholder parameters and grab
26547 matching identifiers. */
26548 tree introduction_list = cp_parser_introduction_list (parser);
26549
26550 /* The introduction-list shall not be empty. */
26551 int nargs = TREE_VEC_LENGTH (introduction_list);
26552 if (nargs == 0)
26553 {
26554 error ("empty introduction-list");
26555 return true;
26556 }
26557
26558 /* Look for closing brace for introduction. */
26559 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26560 return true;
26561
26562 if (tmpl_decl == error_mark_node)
26563 {
26564 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26565 token->location);
26566 return true;
26567 }
26568
26569 /* Build and associate the constraint. */
26570 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26571 if (parms && parms != error_mark_node)
26572 {
26573 cp_parser_template_declaration_after_parameters (parser, parms,
26574 member_p);
26575 return true;
26576 }
26577
26578 error_at (token->location, "no matching concept for template-introduction");
26579 return true;
26580}
26581
26582/* Parse a normal template-declaration following the template keyword. */
26583
26584static void
26585cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26586{
26587 tree parameter_list;
26588 bool need_lang_pop;
26589 location_t location = input_location;
26590
26591 /* Look for the `<' token. */
26592 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26593 return;
26594 if (at_class_scope_p () && current_function_decl)
26595 {
26596 /* 14.5.2.2 [temp.mem]
26597
26598 A local class shall not have member templates. */
26599 error_at (location,
26600 "invalid declaration of member template in local class");
26601 cp_parser_skip_to_end_of_block_or_statement (parser);
26602 return;
26603 }
26604 /* [temp]
26605
26606 A template ... shall not have C linkage. */
26607 if (current_lang_name == lang_name_c)
26608 {
26609 error_at (location, "template with C linkage");
26610 /* Give it C++ linkage to avoid confusing other parts of the
26611 front end. */
26612 push_lang_context (lang_name_cplusplus);
26613 need_lang_pop = true;
26614 }
26615 else
26616 need_lang_pop = false;
26617
26618 /* We cannot perform access checks on the template parameter
26619 declarations until we know what is being declared, just as we
26620 cannot check the decl-specifier list. */
26621 push_deferring_access_checks (dk_deferred);
26622
26623 /* If the next token is `>', then we have an invalid
26624 specialization. Rather than complain about an invalid template
26625 parameter, issue an error message here. */
26626 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26627 {
26628 cp_parser_error (parser, "invalid explicit specialization");
26629 begin_specialization ();
26630 parameter_list = NULL_TREE;
26631 }
26632 else
26633 {
26634 /* Parse the template parameters. */
26635 parameter_list = cp_parser_template_parameter_list (parser);
26636 }
26637
26638 /* Look for the `>'. */
26639 cp_parser_skip_to_end_of_template_parameter_list (parser);
26640
26641 /* Manage template requirements */
26642 if (flag_concepts)
26643 {
26644 tree reqs = get_shorthand_constraints (current_template_parms);
26645 if (tree r = cp_parser_requires_clause_opt (parser))
26646 reqs = conjoin_constraints (reqs, normalize_expression (r));
26647 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26648 }
26649
26650 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26651 member_p);
26652
26653 /* For the erroneous case of a template with C linkage, we pushed an
26654 implicit C++ linkage scope; exit that scope now. */
26655 if (need_lang_pop)
26656 pop_lang_context ();
26657}
26658
26659/* Parse a template-declaration, assuming that the `export' (and
26660 `extern') keywords, if present, has already been scanned. MEMBER_P
26661 is as for cp_parser_template_declaration. */
26662
26663static bool
26664cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26665{
26666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26667 {
26668 cp_lexer_consume_token (parser->lexer);
26669 cp_parser_explicit_template_declaration (parser, member_p);
26670 return true;
26671 }
26672 else if (flag_concepts)
26673 return cp_parser_template_introduction (parser, member_p);
26674
26675 return false;
26676}
26677
26678/* Perform the deferred access checks from a template-parameter-list.
26679 CHECKS is a TREE_LIST of access checks, as returned by
26680 get_deferred_access_checks. */
26681
26682static void
26683cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26684{
26685 ++processing_template_parmlist;
26686 perform_access_checks (checks, tf_warning_or_error);
26687 --processing_template_parmlist;
26688}
26689
26690/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26691 `function-definition' sequence that follows a template header.
26692 If MEMBER_P is true, this declaration appears in a class scope.
26693
26694 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26695 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26696
26697static tree
26698cp_parser_single_declaration (cp_parser* parser,
26699 vec<deferred_access_check, va_gc> *checks,
26700 bool member_p,
26701 bool explicit_specialization_p,
26702 bool* friend_p)
26703{
26704 int declares_class_or_enum;
26705 tree decl = NULL_TREE;
26706 cp_decl_specifier_seq decl_specifiers;
26707 bool function_definition_p = false;
26708 cp_token *decl_spec_token_start;
26709
26710 /* This function is only used when processing a template
26711 declaration. */
26712 gcc_assert (innermost_scope_kind () == sk_template_parms
26713 || innermost_scope_kind () == sk_template_spec);
26714
26715 /* Defer access checks until we know what is being declared. */
26716 push_deferring_access_checks (dk_deferred);
26717
26718 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26719 alternative. */
26720 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26721 cp_parser_decl_specifier_seq (parser,
26722 CP_PARSER_FLAGS_OPTIONAL,
26723 &decl_specifiers,
26724 &declares_class_or_enum);
26725 if (friend_p)
26726 *friend_p = cp_parser_friend_p (&decl_specifiers);
26727
26728 /* There are no template typedefs. */
26729 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26730 {
26731 error_at (decl_spec_token_start->location,
26732 "template declaration of %<typedef%>");
26733 decl = error_mark_node;
26734 }
26735
26736 /* Gather up the access checks that occurred the
26737 decl-specifier-seq. */
26738 stop_deferring_access_checks ();
26739
26740 /* Check for the declaration of a template class. */
26741 if (declares_class_or_enum)
26742 {
26743 if (cp_parser_declares_only_class_p (parser)
26744 || (declares_class_or_enum & 2))
26745 {
26746 // If this is a declaration, but not a definition, associate
26747 // any constraints with the type declaration. Constraints
26748 // are associated with definitions in cp_parser_class_specifier.
26749 if (declares_class_or_enum == 1)
26750 associate_classtype_constraints (decl_specifiers.type);
26751
26752 decl = shadow_tag (&decl_specifiers);
26753
26754 /* In this case:
26755
26756 struct C {
26757 friend template <typename T> struct A<T>::B;
26758 };
26759
26760 A<T>::B will be represented by a TYPENAME_TYPE, and
26761 therefore not recognized by shadow_tag. */
26762 if (friend_p && *friend_p
26763 && !decl
26764 && decl_specifiers.type
26765 && TYPE_P (decl_specifiers.type))
26766 decl = decl_specifiers.type;
26767
26768 if (decl && decl != error_mark_node)
26769 decl = TYPE_NAME (decl);
26770 else
26771 decl = error_mark_node;
26772
26773 /* Perform access checks for template parameters. */
26774 cp_parser_perform_template_parameter_access_checks (checks);
26775
26776 /* Give a helpful diagnostic for
26777 template <class T> struct A { } a;
26778 if we aren't already recovering from an error. */
26779 if (!cp_parser_declares_only_class_p (parser)
26780 && !seen_error ())
26781 {
26782 error_at (cp_lexer_peek_token (parser->lexer)->location,
26783 "a class template declaration must not declare "
26784 "anything else");
26785 cp_parser_skip_to_end_of_block_or_statement (parser);
26786 goto out;
26787 }
26788 }
26789 }
26790
26791 /* Complain about missing 'typename' or other invalid type names. */
26792 if (!decl_specifiers.any_type_specifiers_p
26793 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26794 {
26795 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26796 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26797 the rest of this declaration. */
26798 decl = error_mark_node;
26799 goto out;
26800 }
26801
26802 /* If it's not a template class, try for a template function. If
26803 the next token is a `;', then this declaration does not declare
26804 anything. But, if there were errors in the decl-specifiers, then
26805 the error might well have come from an attempted class-specifier.
26806 In that case, there's no need to warn about a missing declarator. */
26807 if (!decl
26808 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26809 || decl_specifiers.type != error_mark_node))
26810 {
26811 decl = cp_parser_init_declarator (parser,
26812 &decl_specifiers,
26813 checks,
26814 /*function_definition_allowed_p=*/true,
26815 member_p,
26816 declares_class_or_enum,
26817 &function_definition_p,
26818 NULL, NULL, NULL);
26819
26820 /* 7.1.1-1 [dcl.stc]
26821
26822 A storage-class-specifier shall not be specified in an explicit
26823 specialization... */
26824 if (decl
26825 && explicit_specialization_p
26826 && decl_specifiers.storage_class != sc_none)
26827 {
26828 error_at (decl_spec_token_start->location,
26829 "explicit template specialization cannot have a storage class");
26830 decl = error_mark_node;
26831 }
26832
26833 if (decl && VAR_P (decl))
26834 check_template_variable (decl);
26835 }
26836
26837 /* Look for a trailing `;' after the declaration. */
26838 if (!function_definition_p
26839 && (decl == error_mark_node
26840 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26841 cp_parser_skip_to_end_of_block_or_statement (parser);
26842
26843 out:
26844 pop_deferring_access_checks ();
26845
26846 /* Clear any current qualification; whatever comes next is the start
26847 of something new. */
26848 parser->scope = NULL_TREE;
26849 parser->qualifying_scope = NULL_TREE;
26850 parser->object_scope = NULL_TREE;
26851
26852 return decl;
26853}
26854
26855/* Parse a cast-expression that is not the operand of a unary "&". */
26856
26857static cp_expr
26858cp_parser_simple_cast_expression (cp_parser *parser)
26859{
26860 return cp_parser_cast_expression (parser, /*address_p=*/false,
26861 /*cast_p=*/false, /*decltype*/false, NULL);
26862}
26863
26864/* Parse a functional cast to TYPE. Returns an expression
26865 representing the cast. */
26866
26867static cp_expr
26868cp_parser_functional_cast (cp_parser* parser, tree type)
26869{
26870 vec<tree, va_gc> *vec;
26871 tree expression_list;
26872 cp_expr cast;
26873 bool nonconst_p;
26874
26875 location_t start_loc = input_location;
26876
26877 if (!type)
26878 type = error_mark_node;
26879
26880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26881 {
26882 cp_lexer_set_source_position (parser->lexer);
26883 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26884 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26885 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26886 if (TREE_CODE (type) == TYPE_DECL)
26887 type = TREE_TYPE (type);
26888
26889 cast = finish_compound_literal (type, expression_list,
26890 tf_warning_or_error);
26891 /* Create a location of the form:
26892 type_name{i, f}
26893 ^~~~~~~~~~~~~~~
26894 with caret == start at the start of the type name,
26895 finishing at the closing brace. */
26896 location_t finish_loc
26897 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26898 location_t combined_loc = make_location (start_loc, start_loc,
26899 finish_loc);
26900 cast.set_location (combined_loc);
26901 return cast;
26902 }
26903
26904
26905 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26906 /*cast_p=*/true,
26907 /*allow_expansion_p=*/true,
26908 /*non_constant_p=*/NULL);
26909 if (vec == NULL)
26910 expression_list = error_mark_node;
26911 else
26912 {
26913 expression_list = build_tree_list_vec (vec);
26914 release_tree_vector (vec);
26915 }
26916
26917 cast = build_functional_cast (type, expression_list,
26918 tf_warning_or_error);
26919 /* [expr.const]/1: In an integral constant expression "only type
26920 conversions to integral or enumeration type can be used". */
26921 if (TREE_CODE (type) == TYPE_DECL)
26922 type = TREE_TYPE (type);
26923 if (cast != error_mark_node
26924 && !cast_valid_in_integral_constant_expression_p (type)
26925 && cp_parser_non_integral_constant_expression (parser,
26926 NIC_CONSTRUCTOR))
26927 return error_mark_node;
26928
26929 /* Create a location of the form:
26930 float(i)
26931 ^~~~~~~~
26932 with caret == start at the start of the type name,
26933 finishing at the closing paren. */
26934 location_t finish_loc
26935 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26936 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26937 cast.set_location (combined_loc);
26938 return cast;
26939}
26940
26941/* Save the tokens that make up the body of a member function defined
26942 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26943 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26944 specifiers applied to the declaration. Returns the FUNCTION_DECL
26945 for the member function. */
26946
26947static tree
26948cp_parser_save_member_function_body (cp_parser* parser,
26949 cp_decl_specifier_seq *decl_specifiers,
26950 cp_declarator *declarator,
26951 tree attributes)
26952{
26953 cp_token *first;
26954 cp_token *last;
26955 tree fn;
26956 bool function_try_block = false;
26957
26958 /* Create the FUNCTION_DECL. */
26959 fn = grokmethod (decl_specifiers, declarator, attributes);
26960 cp_finalize_omp_declare_simd (parser, fn);
26961 cp_finalize_oacc_routine (parser, fn, true);
26962 /* If something went badly wrong, bail out now. */
26963 if (fn == error_mark_node)
26964 {
26965 /* If there's a function-body, skip it. */
26966 if (cp_parser_token_starts_function_definition_p
26967 (cp_lexer_peek_token (parser->lexer)))
26968 cp_parser_skip_to_end_of_block_or_statement (parser);
26969 return error_mark_node;
26970 }
26971
26972 /* Remember it, if there default args to post process. */
26973 cp_parser_save_default_args (parser, fn);
26974
26975 /* Save away the tokens that make up the body of the
26976 function. */
26977 first = parser->lexer->next_token;
26978
26979 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26980 cp_lexer_consume_token (parser->lexer);
26981 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26982 RID_TRANSACTION_ATOMIC))
26983 {
26984 cp_lexer_consume_token (parser->lexer);
26985 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26987 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26988 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26989 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26990 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26991 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26992 {
26993 cp_lexer_consume_token (parser->lexer);
26994 cp_lexer_consume_token (parser->lexer);
26995 cp_lexer_consume_token (parser->lexer);
26996 cp_lexer_consume_token (parser->lexer);
26997 cp_lexer_consume_token (parser->lexer);
26998 }
26999 else
27000 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27001 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27002 {
27003 cp_lexer_consume_token (parser->lexer);
27004 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27005 break;
27006 }
27007 }
27008
27009 /* Handle function try blocks. */
27010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27011 {
27012 cp_lexer_consume_token (parser->lexer);
27013 function_try_block = true;
27014 }
27015 /* We can have braced-init-list mem-initializers before the fn body. */
27016 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27017 {
27018 cp_lexer_consume_token (parser->lexer);
27019 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27020 {
27021 /* cache_group will stop after an un-nested { } pair, too. */
27022 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27023 break;
27024
27025 /* variadic mem-inits have ... after the ')'. */
27026 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27027 cp_lexer_consume_token (parser->lexer);
27028 }
27029 }
27030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27031 /* Handle function try blocks. */
27032 if (function_try_block)
27033 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27034 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27035 last = parser->lexer->next_token;
27036
27037 /* Save away the inline definition; we will process it when the
27038 class is complete. */
27039 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27040 DECL_PENDING_INLINE_P (fn) = 1;
27041
27042 /* We need to know that this was defined in the class, so that
27043 friend templates are handled correctly. */
27044 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27045
27046 /* Add FN to the queue of functions to be parsed later. */
27047 vec_safe_push (unparsed_funs_with_definitions, fn);
27048
27049 return fn;
27050}
27051
27052/* Save the tokens that make up the in-class initializer for a non-static
27053 data member. Returns a DEFAULT_ARG. */
27054
27055static tree
27056cp_parser_save_nsdmi (cp_parser* parser)
27057{
27058 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27059}
27060
27061/* Parse a template-argument-list, as well as the trailing ">" (but
27062 not the opening "<"). See cp_parser_template_argument_list for the
27063 return value. */
27064
27065static tree
27066cp_parser_enclosed_template_argument_list (cp_parser* parser)
27067{
27068 tree arguments;
27069 tree saved_scope;
27070 tree saved_qualifying_scope;
27071 tree saved_object_scope;
27072 bool saved_greater_than_is_operator_p;
27073 int saved_unevaluated_operand;
27074 int saved_inhibit_evaluation_warnings;
27075
27076 /* [temp.names]
27077
27078 When parsing a template-id, the first non-nested `>' is taken as
27079 the end of the template-argument-list rather than a greater-than
27080 operator. */
27081 saved_greater_than_is_operator_p
27082 = parser->greater_than_is_operator_p;
27083 parser->greater_than_is_operator_p = false;
27084 /* Parsing the argument list may modify SCOPE, so we save it
27085 here. */
27086 saved_scope = parser->scope;
27087 saved_qualifying_scope = parser->qualifying_scope;
27088 saved_object_scope = parser->object_scope;
27089 /* We need to evaluate the template arguments, even though this
27090 template-id may be nested within a "sizeof". */
27091 saved_unevaluated_operand = cp_unevaluated_operand;
27092 cp_unevaluated_operand = 0;
27093 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27094 c_inhibit_evaluation_warnings = 0;
27095 /* Parse the template-argument-list itself. */
27096 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27097 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27098 arguments = NULL_TREE;
27099 else
27100 arguments = cp_parser_template_argument_list (parser);
27101 /* Look for the `>' that ends the template-argument-list. If we find
27102 a '>>' instead, it's probably just a typo. */
27103 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27104 {
27105 if (cxx_dialect != cxx98)
27106 {
27107 /* In C++0x, a `>>' in a template argument list or cast
27108 expression is considered to be two separate `>'
27109 tokens. So, change the current token to a `>', but don't
27110 consume it: it will be consumed later when the outer
27111 template argument list (or cast expression) is parsed.
27112 Note that this replacement of `>' for `>>' is necessary
27113 even if we are parsing tentatively: in the tentative
27114 case, after calling
27115 cp_parser_enclosed_template_argument_list we will always
27116 throw away all of the template arguments and the first
27117 closing `>', either because the template argument list
27118 was erroneous or because we are replacing those tokens
27119 with a CPP_TEMPLATE_ID token. The second `>' (which will
27120 not have been thrown away) is needed either to close an
27121 outer template argument list or to complete a new-style
27122 cast. */
27123 cp_token *token = cp_lexer_peek_token (parser->lexer);
27124 token->type = CPP_GREATER;
27125 }
27126 else if (!saved_greater_than_is_operator_p)
27127 {
27128 /* If we're in a nested template argument list, the '>>' has
27129 to be a typo for '> >'. We emit the error message, but we
27130 continue parsing and we push a '>' as next token, so that
27131 the argument list will be parsed correctly. Note that the
27132 global source location is still on the token before the
27133 '>>', so we need to say explicitly where we want it. */
27134 cp_token *token = cp_lexer_peek_token (parser->lexer);
27135 gcc_rich_location richloc (token->location);
27136 richloc.add_fixit_replace ("> >");
27137 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27138 "within a nested template argument list");
27139
27140 token->type = CPP_GREATER;
27141 }
27142 else
27143 {
27144 /* If this is not a nested template argument list, the '>>'
27145 is a typo for '>'. Emit an error message and continue.
27146 Same deal about the token location, but here we can get it
27147 right by consuming the '>>' before issuing the diagnostic. */
27148 cp_token *token = cp_lexer_consume_token (parser->lexer);
27149 error_at (token->location,
27150 "spurious %<>>%>, use %<>%> to terminate "
27151 "a template argument list");
27152 }
27153 }
27154 else
27155 cp_parser_skip_to_end_of_template_parameter_list (parser);
27156 /* The `>' token might be a greater-than operator again now. */
27157 parser->greater_than_is_operator_p
27158 = saved_greater_than_is_operator_p;
27159 /* Restore the SAVED_SCOPE. */
27160 parser->scope = saved_scope;
27161 parser->qualifying_scope = saved_qualifying_scope;
27162 parser->object_scope = saved_object_scope;
27163 cp_unevaluated_operand = saved_unevaluated_operand;
27164 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27165
27166 return arguments;
27167}
27168
27169/* MEMBER_FUNCTION is a member function, or a friend. If default
27170 arguments, or the body of the function have not yet been parsed,
27171 parse them now. */
27172
27173static void
27174cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27175{
27176 timevar_push (TV_PARSE_INMETH);
27177 /* If this member is a template, get the underlying
27178 FUNCTION_DECL. */
27179 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27180 member_function = DECL_TEMPLATE_RESULT (member_function);
27181
27182 /* There should not be any class definitions in progress at this
27183 point; the bodies of members are only parsed outside of all class
27184 definitions. */
27185 gcc_assert (parser->num_classes_being_defined == 0);
27186 /* While we're parsing the member functions we might encounter more
27187 classes. We want to handle them right away, but we don't want
27188 them getting mixed up with functions that are currently in the
27189 queue. */
27190 push_unparsed_function_queues (parser);
27191
27192 /* Make sure that any template parameters are in scope. */
27193 maybe_begin_member_template_processing (member_function);
27194
27195 /* If the body of the function has not yet been parsed, parse it
27196 now. */
27197 if (DECL_PENDING_INLINE_P (member_function))
27198 {
27199 tree function_scope;
27200 cp_token_cache *tokens;
27201
27202 /* The function is no longer pending; we are processing it. */
27203 tokens = DECL_PENDING_INLINE_INFO (member_function);
27204 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27205 DECL_PENDING_INLINE_P (member_function) = 0;
27206
27207 /* If this is a local class, enter the scope of the containing
27208 function. */
27209 function_scope = current_function_decl;
27210 if (function_scope)
27211 push_function_context ();
27212
27213 /* Push the body of the function onto the lexer stack. */
27214 cp_parser_push_lexer_for_tokens (parser, tokens);
27215
27216 /* Let the front end know that we going to be defining this
27217 function. */
27218 start_preparsed_function (member_function, NULL_TREE,
27219 SF_PRE_PARSED | SF_INCLASS_INLINE);
27220
27221 /* Don't do access checking if it is a templated function. */
27222 if (processing_template_decl)
27223 push_deferring_access_checks (dk_no_check);
27224
27225 /* #pragma omp declare reduction needs special parsing. */
27226 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27227 {
27228 parser->lexer->in_pragma = true;
27229 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27230 finish_function (/*inline*/2);
27231 cp_check_omp_declare_reduction (member_function);
27232 }
27233 else
27234 /* Now, parse the body of the function. */
27235 cp_parser_function_definition_after_declarator (parser,
27236 /*inline_p=*/true);
27237
27238 if (processing_template_decl)
27239 pop_deferring_access_checks ();
27240
27241 /* Leave the scope of the containing function. */
27242 if (function_scope)
27243 pop_function_context ();
27244 cp_parser_pop_lexer (parser);
27245 }
27246
27247 /* Remove any template parameters from the symbol table. */
27248 maybe_end_member_template_processing ();
27249
27250 /* Restore the queue. */
27251 pop_unparsed_function_queues (parser);
27252 timevar_pop (TV_PARSE_INMETH);
27253}
27254
27255/* If DECL contains any default args, remember it on the unparsed
27256 functions queue. */
27257
27258static void
27259cp_parser_save_default_args (cp_parser* parser, tree decl)
27260{
27261 tree probe;
27262
27263 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27264 probe;
27265 probe = TREE_CHAIN (probe))
27266 if (TREE_PURPOSE (probe))
27267 {
27268 cp_default_arg_entry entry = {current_class_type, decl};
27269 vec_safe_push (unparsed_funs_with_default_args, entry);
27270 break;
27271 }
27272}
27273
27274/* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27275 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27276 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27277 from the parameter-type-list. */
27278
27279static tree
27280cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27281 tree default_arg, tree parmtype)
27282{
27283 cp_token_cache *tokens;
27284 tree parsed_arg;
27285 bool dummy;
27286
27287 if (default_arg == error_mark_node)
27288 return error_mark_node;
27289
27290 /* Push the saved tokens for the default argument onto the parser's
27291 lexer stack. */
27292 tokens = DEFARG_TOKENS (default_arg);
27293 cp_parser_push_lexer_for_tokens (parser, tokens);
27294
27295 start_lambda_scope (decl);
27296
27297 /* Parse the default argument. */
27298 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27299 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27300 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27301
27302 finish_lambda_scope ();
27303
27304 if (parsed_arg == error_mark_node)
27305 cp_parser_skip_to_end_of_statement (parser);
27306
27307 if (!processing_template_decl)
27308 {
27309 /* In a non-template class, check conversions now. In a template,
27310 we'll wait and instantiate these as needed. */
27311 if (TREE_CODE (decl) == PARM_DECL)
27312 parsed_arg = check_default_argument (parmtype, parsed_arg,
27313 tf_warning_or_error);
27314 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27315 parsed_arg = error_mark_node;
27316 else
27317 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27318 }
27319
27320 /* If the token stream has not been completely used up, then
27321 there was extra junk after the end of the default
27322 argument. */
27323 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27324 {
27325 if (TREE_CODE (decl) == PARM_DECL)
27326 cp_parser_error (parser, "expected %<,%>");
27327 else
27328 cp_parser_error (parser, "expected %<;%>");
27329 }
27330
27331 /* Revert to the main lexer. */
27332 cp_parser_pop_lexer (parser);
27333
27334 return parsed_arg;
27335}
27336
27337/* FIELD is a non-static data member with an initializer which we saved for
27338 later; parse it now. */
27339
27340static void
27341cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27342{
27343 tree def;
27344
27345 maybe_begin_member_template_processing (field);
27346
27347 push_unparsed_function_queues (parser);
27348 def = cp_parser_late_parse_one_default_arg (parser, field,
27349 DECL_INITIAL (field),
27350 NULL_TREE);
27351 pop_unparsed_function_queues (parser);
27352
27353 maybe_end_member_template_processing ();
27354
27355 DECL_INITIAL (field) = def;
27356}
27357
27358/* FN is a FUNCTION_DECL which may contains a parameter with an
27359 unparsed DEFAULT_ARG. Parse the default args now. This function
27360 assumes that the current scope is the scope in which the default
27361 argument should be processed. */
27362
27363static void
27364cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27365{
27366 bool saved_local_variables_forbidden_p;
27367 tree parm, parmdecl;
27368
27369 /* While we're parsing the default args, we might (due to the
27370 statement expression extension) encounter more classes. We want
27371 to handle them right away, but we don't want them getting mixed
27372 up with default args that are currently in the queue. */
27373 push_unparsed_function_queues (parser);
27374
27375 /* Local variable names (and the `this' keyword) may not appear
27376 in a default argument. */
27377 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27378 parser->local_variables_forbidden_p = true;
27379
27380 push_defarg_context (fn);
27381
27382 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27383 parmdecl = DECL_ARGUMENTS (fn);
27384 parm && parm != void_list_node;
27385 parm = TREE_CHAIN (parm),
27386 parmdecl = DECL_CHAIN (parmdecl))
27387 {
27388 tree default_arg = TREE_PURPOSE (parm);
27389 tree parsed_arg;
27390 vec<tree, va_gc> *insts;
27391 tree copy;
27392 unsigned ix;
27393
27394 if (!default_arg)
27395 continue;
27396
27397 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27398 /* This can happen for a friend declaration for a function
27399 already declared with default arguments. */
27400 continue;
27401
27402 parsed_arg
27403 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27404 default_arg,
27405 TREE_VALUE (parm));
27406 if (parsed_arg == error_mark_node)
27407 {
27408 continue;
27409 }
27410
27411 TREE_PURPOSE (parm) = parsed_arg;
27412
27413 /* Update any instantiations we've already created. */
27414 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27415 vec_safe_iterate (insts, ix, &copy); ix++)
27416 TREE_PURPOSE (copy) = parsed_arg;
27417 }
27418
27419 pop_defarg_context ();
27420
27421 /* Make sure no default arg is missing. */
27422 check_default_args (fn);
27423
27424 /* Restore the state of local_variables_forbidden_p. */
27425 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27426
27427 /* Restore the queue. */
27428 pop_unparsed_function_queues (parser);
27429}
27430
27431/* Subroutine of cp_parser_sizeof_operand, for handling C++11
27432
27433 sizeof ... ( identifier )
27434
27435 where the 'sizeof' token has already been consumed. */
27436
27437static tree
27438cp_parser_sizeof_pack (cp_parser *parser)
27439{
27440 /* Consume the `...'. */
27441 cp_lexer_consume_token (parser->lexer);
27442 maybe_warn_variadic_templates ();
27443
27444 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27445 if (paren)
27446 cp_lexer_consume_token (parser->lexer);
27447 else
27448 permerror (cp_lexer_peek_token (parser->lexer)->location,
27449 "%<sizeof...%> argument must be surrounded by parentheses");
27450
27451 cp_token *token = cp_lexer_peek_token (parser->lexer);
27452 tree name = cp_parser_identifier (parser);
27453 if (name == error_mark_node)
27454 return error_mark_node;
27455 /* The name is not qualified. */
27456 parser->scope = NULL_TREE;
27457 parser->qualifying_scope = NULL_TREE;
27458 parser->object_scope = NULL_TREE;
27459 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27460 if (expr == error_mark_node)
27461 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27462 token->location);
27463 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27464 expr = TREE_TYPE (expr);
27465 else if (TREE_CODE (expr) == CONST_DECL)
27466 expr = DECL_INITIAL (expr);
27467 expr = make_pack_expansion (expr);
27468 PACK_EXPANSION_SIZEOF_P (expr) = true;
27469
27470 if (paren)
27471 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27472
27473 return expr;
27474}
27475
27476/* Parse the operand of `sizeof' (or a similar operator). Returns
27477 either a TYPE or an expression, depending on the form of the
27478 input. The KEYWORD indicates which kind of expression we have
27479 encountered. */
27480
27481static tree
27482cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27483{
27484 tree expr = NULL_TREE;
27485 const char *saved_message;
27486 char *tmp;
27487 bool saved_integral_constant_expression_p;
27488 bool saved_non_integral_constant_expression_p;
27489
27490 /* If it's a `...', then we are computing the length of a parameter
27491 pack. */
27492 if (keyword == RID_SIZEOF
27493 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27494 return cp_parser_sizeof_pack (parser);
27495
27496 /* Types cannot be defined in a `sizeof' expression. Save away the
27497 old message. */
27498 saved_message = parser->type_definition_forbidden_message;
27499 /* And create the new one. */
27500 tmp = concat ("types may not be defined in %<",
27501 IDENTIFIER_POINTER (ridpointers[keyword]),
27502 "%> expressions", NULL);
27503 parser->type_definition_forbidden_message = tmp;
27504
27505 /* The restrictions on constant-expressions do not apply inside
27506 sizeof expressions. */
27507 saved_integral_constant_expression_p
27508 = parser->integral_constant_expression_p;
27509 saved_non_integral_constant_expression_p
27510 = parser->non_integral_constant_expression_p;
27511 parser->integral_constant_expression_p = false;
27512
27513 /* Do not actually evaluate the expression. */
27514 ++cp_unevaluated_operand;
27515 ++c_inhibit_evaluation_warnings;
27516 /* If it's a `(', then we might be looking at the type-id
27517 construction. */
27518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27519 {
27520 tree type = NULL_TREE;
27521
27522 /* We can't be sure yet whether we're looking at a type-id or an
27523 expression. */
27524 cp_parser_parse_tentatively (parser);
27525 /* Note: as a GNU Extension, compound literals are considered
27526 postfix-expressions as they are in C99, so they are valid
27527 arguments to sizeof. See comment in cp_parser_cast_expression
27528 for details. */
27529 if (cp_parser_compound_literal_p (parser))
27530 cp_parser_simulate_error (parser);
27531 else
27532 {
27533 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27534 parser->in_type_id_in_expr_p = true;
27535 /* Look for the type-id. */
27536 type = cp_parser_type_id (parser);
27537 /* Look for the closing `)'. */
27538 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27539 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27540 }
27541
27542 /* If all went well, then we're done. */
27543 if (cp_parser_parse_definitely (parser))
27544 {
27545 cp_decl_specifier_seq decl_specs;
27546
27547 /* Build a trivial decl-specifier-seq. */
27548 clear_decl_specs (&decl_specs);
27549 decl_specs.type = type;
27550
27551 /* Call grokdeclarator to figure out what type this is. */
27552 expr = grokdeclarator (NULL,
27553 &decl_specs,
27554 TYPENAME,
27555 /*initialized=*/0,
27556 /*attrlist=*/NULL);
27557 }
27558 }
27559
27560 /* If the type-id production did not work out, then we must be
27561 looking at the unary-expression production. */
27562 if (!expr)
27563 expr = cp_parser_unary_expression (parser);
27564
27565 /* Go back to evaluating expressions. */
27566 --cp_unevaluated_operand;
27567 --c_inhibit_evaluation_warnings;
27568
27569 /* Free the message we created. */
27570 free (tmp);
27571 /* And restore the old one. */
27572 parser->type_definition_forbidden_message = saved_message;
27573 parser->integral_constant_expression_p
27574 = saved_integral_constant_expression_p;
27575 parser->non_integral_constant_expression_p
27576 = saved_non_integral_constant_expression_p;
27577
27578 return expr;
27579}
27580
27581/* If the current declaration has no declarator, return true. */
27582
27583static bool
27584cp_parser_declares_only_class_p (cp_parser *parser)
27585{
27586 /* If the next token is a `;' or a `,' then there is no
27587 declarator. */
27588 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27589 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27590}
27591
27592/* Update the DECL_SPECS to reflect the storage class indicated by
27593 KEYWORD. */
27594
27595static void
27596cp_parser_set_storage_class (cp_parser *parser,
27597 cp_decl_specifier_seq *decl_specs,
27598 enum rid keyword,
27599 cp_token *token)
27600{
27601 cp_storage_class storage_class;
27602
27603 if (parser->in_unbraced_linkage_specification_p)
27604 {
27605 error_at (token->location, "invalid use of %qD in linkage specification",
27606 ridpointers[keyword]);
27607 return;
27608 }
27609 else if (decl_specs->storage_class != sc_none)
27610 {
27611 decl_specs->conflicting_specifiers_p = true;
27612 return;
27613 }
27614
27615 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27616 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27617 && decl_specs->gnu_thread_keyword_p)
27618 {
27619 pedwarn (decl_specs->locations[ds_thread], 0,
27620 "%<__thread%> before %qD", ridpointers[keyword]);
27621 }
27622
27623 switch (keyword)
27624 {
27625 case RID_AUTO:
27626 storage_class = sc_auto;
27627 break;
27628 case RID_REGISTER:
27629 storage_class = sc_register;
27630 break;
27631 case RID_STATIC:
27632 storage_class = sc_static;
27633 break;
27634 case RID_EXTERN:
27635 storage_class = sc_extern;
27636 break;
27637 case RID_MUTABLE:
27638 storage_class = sc_mutable;
27639 break;
27640 default:
27641 gcc_unreachable ();
27642 }
27643 decl_specs->storage_class = storage_class;
27644 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27645
27646 /* A storage class specifier cannot be applied alongside a typedef
27647 specifier. If there is a typedef specifier present then set
27648 conflicting_specifiers_p which will trigger an error later
27649 on in grokdeclarator. */
27650 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27651 decl_specs->conflicting_specifiers_p = true;
27652}
27653
27654/* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27655 is true, the type is a class or enum definition. */
27656
27657static void
27658cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27659 tree type_spec,
27660 cp_token *token,
27661 bool type_definition_p)
27662{
27663 decl_specs->any_specifiers_p = true;
27664
27665 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27666 (with, for example, in "typedef int wchar_t;") we remember that
27667 this is what happened. In system headers, we ignore these
27668 declarations so that G++ can work with system headers that are not
27669 C++-safe. */
27670 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27671 && !type_definition_p
27672 && (type_spec == boolean_type_node
27673 || type_spec == char16_type_node
27674 || type_spec == char32_type_node
27675 || type_spec == wchar_type_node)
27676 && (decl_specs->type
27677 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27678 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27679 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27680 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27681 {
27682 decl_specs->redefined_builtin_type = type_spec;
27683 set_and_check_decl_spec_loc (decl_specs,
27684 ds_redefined_builtin_type_spec,
27685 token);
27686 if (!decl_specs->type)
27687 {
27688 decl_specs->type = type_spec;
27689 decl_specs->type_definition_p = false;
27690 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27691 }
27692 }
27693 else if (decl_specs->type)
27694 decl_specs->multiple_types_p = true;
27695 else
27696 {
27697 decl_specs->type = type_spec;
27698 decl_specs->type_definition_p = type_definition_p;
27699 decl_specs->redefined_builtin_type = NULL_TREE;
27700 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27701 }
27702}
27703
27704/* True iff TOKEN is the GNU keyword __thread. */
27705
27706static bool
27707token_is__thread (cp_token *token)
27708{
27709 gcc_assert (token->keyword == RID_THREAD);
27710 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27711}
27712
27713/* Set the location for a declarator specifier and check if it is
27714 duplicated.
27715
27716 DECL_SPECS is the sequence of declarator specifiers onto which to
27717 set the location.
27718
27719 DS is the single declarator specifier to set which location is to
27720 be set onto the existing sequence of declarators.
27721
27722 LOCATION is the location for the declarator specifier to
27723 consider. */
27724
27725static void
27726set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27727 cp_decl_spec ds, cp_token *token)
27728{
27729 gcc_assert (ds < ds_last);
27730
27731 if (decl_specs == NULL)
27732 return;
27733
27734 source_location location = token->location;
27735
27736 if (decl_specs->locations[ds] == 0)
27737 {
27738 decl_specs->locations[ds] = location;
27739 if (ds == ds_thread)
27740 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27741 }
27742 else
27743 {
27744 if (ds == ds_long)
27745 {
27746 if (decl_specs->locations[ds_long_long] != 0)
27747 error_at (location,
27748 "%<long long long%> is too long for GCC");
27749 else
27750 {
27751 decl_specs->locations[ds_long_long] = location;
27752 pedwarn_cxx98 (location,
27753 OPT_Wlong_long,
27754 "ISO C++ 1998 does not support %<long long%>");
27755 }
27756 }
27757 else if (ds == ds_thread)
27758 {
27759 bool gnu = token_is__thread (token);
27760 if (gnu != decl_specs->gnu_thread_keyword_p)
27761 error_at (location,
27762 "both %<__thread%> and %<thread_local%> specified");
27763 else
27764 error_at (location, "duplicate %qD", token->u.value);
27765 }
27766 else
27767 {
27768 static const char *const decl_spec_names[] = {
27769 "signed",
27770 "unsigned",
27771 "short",
27772 "long",
27773 "const",
27774 "volatile",
27775 "restrict",
27776 "inline",
27777 "virtual",
27778 "explicit",
27779 "friend",
27780 "typedef",
27781 "using",
27782 "constexpr",
27783 "__complex"
27784 };
27785 error_at (location,
27786 "duplicate %qs", decl_spec_names[ds]);
27787 }
27788 }
27789}
27790
27791/* Return true iff the declarator specifier DS is present in the
27792 sequence of declarator specifiers DECL_SPECS. */
27793
27794bool
27795decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27796 cp_decl_spec ds)
27797{
27798 gcc_assert (ds < ds_last);
27799
27800 if (decl_specs == NULL)
27801 return false;
27802
27803 return decl_specs->locations[ds] != 0;
27804}
27805
27806/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27807 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27808
27809static bool
27810cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27811{
27812 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27813}
27814
27815/* Issue an error message indicating that TOKEN_DESC was expected.
27816 If KEYWORD is true, it indicated this function is called by
27817 cp_parser_require_keword and the required token can only be
27818 a indicated keyword. */
27819
27820static void
27821cp_parser_required_error (cp_parser *parser,
27822 required_token token_desc,
27823 bool keyword)
27824{
27825 switch (token_desc)
27826 {
27827 case RT_NEW:
27828 cp_parser_error (parser, "expected %<new%>");
27829 return;
27830 case RT_DELETE:
27831 cp_parser_error (parser, "expected %<delete%>");
27832 return;
27833 case RT_RETURN:
27834 cp_parser_error (parser, "expected %<return%>");
27835 return;
27836 case RT_WHILE:
27837 cp_parser_error (parser, "expected %<while%>");
27838 return;
27839 case RT_EXTERN:
27840 cp_parser_error (parser, "expected %<extern%>");
27841 return;
27842 case RT_STATIC_ASSERT:
27843 cp_parser_error (parser, "expected %<static_assert%>");
27844 return;
27845 case RT_DECLTYPE:
27846 cp_parser_error (parser, "expected %<decltype%>");
27847 return;
27848 case RT_OPERATOR:
27849 cp_parser_error (parser, "expected %<operator%>");
27850 return;
27851 case RT_CLASS:
27852 cp_parser_error (parser, "expected %<class%>");
27853 return;
27854 case RT_TEMPLATE:
27855 cp_parser_error (parser, "expected %<template%>");
27856 return;
27857 case RT_NAMESPACE:
27858 cp_parser_error (parser, "expected %<namespace%>");
27859 return;
27860 case RT_USING:
27861 cp_parser_error (parser, "expected %<using%>");
27862 return;
27863 case RT_ASM:
27864 cp_parser_error (parser, "expected %<asm%>");
27865 return;
27866 case RT_TRY:
27867 cp_parser_error (parser, "expected %<try%>");
27868 return;
27869 case RT_CATCH:
27870 cp_parser_error (parser, "expected %<catch%>");
27871 return;
27872 case RT_THROW:
27873 cp_parser_error (parser, "expected %<throw%>");
27874 return;
27875 case RT_LABEL:
27876 cp_parser_error (parser, "expected %<__label__%>");
27877 return;
27878 case RT_AT_TRY:
27879 cp_parser_error (parser, "expected %<@try%>");
27880 return;
27881 case RT_AT_SYNCHRONIZED:
27882 cp_parser_error (parser, "expected %<@synchronized%>");
27883 return;
27884 case RT_AT_THROW:
27885 cp_parser_error (parser, "expected %<@throw%>");
27886 return;
27887 case RT_TRANSACTION_ATOMIC:
27888 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27889 return;
27890 case RT_TRANSACTION_RELAXED:
27891 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27892 return;
27893 default:
27894 break;
27895 }
27896 if (!keyword)
27897 {
27898 switch (token_desc)
27899 {
27900 case RT_SEMICOLON:
27901 cp_parser_error (parser, "expected %<;%>");
27902 return;
27903 case RT_OPEN_PAREN:
27904 cp_parser_error (parser, "expected %<(%>");
27905 return;
27906 case RT_CLOSE_BRACE:
27907 cp_parser_error (parser, "expected %<}%>");
27908 return;
27909 case RT_OPEN_BRACE:
27910 cp_parser_error (parser, "expected %<{%>");
27911 return;
27912 case RT_CLOSE_SQUARE:
27913 cp_parser_error (parser, "expected %<]%>");
27914 return;
27915 case RT_OPEN_SQUARE:
27916 cp_parser_error (parser, "expected %<[%>");
27917 return;
27918 case RT_COMMA:
27919 cp_parser_error (parser, "expected %<,%>");
27920 return;
27921 case RT_SCOPE:
27922 cp_parser_error (parser, "expected %<::%>");
27923 return;
27924 case RT_LESS:
27925 cp_parser_error (parser, "expected %<<%>");
27926 return;
27927 case RT_GREATER:
27928 cp_parser_error (parser, "expected %<>%>");
27929 return;
27930 case RT_EQ:
27931 cp_parser_error (parser, "expected %<=%>");
27932 return;
27933 case RT_ELLIPSIS:
27934 cp_parser_error (parser, "expected %<...%>");
27935 return;
27936 case RT_MULT:
27937 cp_parser_error (parser, "expected %<*%>");
27938 return;
27939 case RT_COMPL:
27940 cp_parser_error (parser, "expected %<~%>");
27941 return;
27942 case RT_COLON:
27943 cp_parser_error (parser, "expected %<:%>");
27944 return;
27945 case RT_COLON_SCOPE:
27946 cp_parser_error (parser, "expected %<:%> or %<::%>");
27947 return;
27948 case RT_CLOSE_PAREN:
27949 cp_parser_error (parser, "expected %<)%>");
27950 return;
27951 case RT_COMMA_CLOSE_PAREN:
27952 cp_parser_error (parser, "expected %<,%> or %<)%>");
27953 return;
27954 case RT_PRAGMA_EOL:
27955 cp_parser_error (parser, "expected end of line");
27956 return;
27957 case RT_NAME:
27958 cp_parser_error (parser, "expected identifier");
27959 return;
27960 case RT_SELECT:
27961 cp_parser_error (parser, "expected selection-statement");
27962 return;
27963 case RT_INTERATION:
27964 cp_parser_error (parser, "expected iteration-statement");
27965 return;
27966 case RT_JUMP:
27967 cp_parser_error (parser, "expected jump-statement");
27968 return;
27969 case RT_CLASS_KEY:
27970 cp_parser_error (parser, "expected class-key");
27971 return;
27972 case RT_CLASS_TYPENAME_TEMPLATE:
27973 cp_parser_error (parser,
27974 "expected %<class%>, %<typename%>, or %<template%>");
27975 return;
27976 default:
27977 gcc_unreachable ();
27978 }
27979 }
27980 else
27981 gcc_unreachable ();
27982}
27983
27984
27985
27986/* If the next token is of the indicated TYPE, consume it. Otherwise,
27987 issue an error message indicating that TOKEN_DESC was expected.
27988
27989 Returns the token consumed, if the token had the appropriate type.
27990 Otherwise, returns NULL. */
27991
27992static cp_token *
27993cp_parser_require (cp_parser* parser,
27994 enum cpp_ttype type,
27995 required_token token_desc)
27996{
27997 if (cp_lexer_next_token_is (parser->lexer, type))
27998 return cp_lexer_consume_token (parser->lexer);
27999 else
28000 {
28001 /* Output the MESSAGE -- unless we're parsing tentatively. */
28002 if (!cp_parser_simulate_error (parser))
28003 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
28004 return NULL;
28005 }
28006}
28007
28008/* An error message is produced if the next token is not '>'.
28009 All further tokens are skipped until the desired token is
28010 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28011
28012static void
28013cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28014{
28015 /* Current level of '< ... >'. */
28016 unsigned level = 0;
28017 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28018 unsigned nesting_depth = 0;
28019
28020 /* Are we ready, yet? If not, issue error message. */
28021 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28022 return;
28023
28024 /* Skip tokens until the desired token is found. */
28025 while (true)
28026 {
28027 /* Peek at the next token. */
28028 switch (cp_lexer_peek_token (parser->lexer)->type)
28029 {
28030 case CPP_LESS:
28031 if (!nesting_depth)
28032 ++level;
28033 break;
28034
28035 case CPP_RSHIFT:
28036 if (cxx_dialect == cxx98)
28037 /* C++0x views the `>>' operator as two `>' tokens, but
28038 C++98 does not. */
28039 break;
28040 else if (!nesting_depth && level-- == 0)
28041 {
28042 /* We've hit a `>>' where the first `>' closes the
28043 template argument list, and the second `>' is
28044 spurious. Just consume the `>>' and stop; we've
28045 already produced at least one error. */
28046 cp_lexer_consume_token (parser->lexer);
28047 return;
28048 }
28049 /* Fall through for C++0x, so we handle the second `>' in
28050 the `>>'. */
28051 gcc_fallthrough ();
28052
28053 case CPP_GREATER:
28054 if (!nesting_depth && level-- == 0)
28055 {
28056 /* We've reached the token we want, consume it and stop. */
28057 cp_lexer_consume_token (parser->lexer);
28058 return;
28059 }
28060 break;
28061
28062 case CPP_OPEN_PAREN:
28063 case CPP_OPEN_SQUARE:
28064 ++nesting_depth;
28065 break;
28066
28067 case CPP_CLOSE_PAREN:
28068 case CPP_CLOSE_SQUARE:
28069 if (nesting_depth-- == 0)
28070 return;
28071 break;
28072
28073 case CPP_EOF:
28074 case CPP_PRAGMA_EOL:
28075 case CPP_SEMICOLON:
28076 case CPP_OPEN_BRACE:
28077 case CPP_CLOSE_BRACE:
28078 /* The '>' was probably forgotten, don't look further. */
28079 return;
28080
28081 default:
28082 break;
28083 }
28084
28085 /* Consume this token. */
28086 cp_lexer_consume_token (parser->lexer);
28087 }
28088}
28089
28090/* If the next token is the indicated keyword, consume it. Otherwise,
28091 issue an error message indicating that TOKEN_DESC was expected.
28092
28093 Returns the token consumed, if the token had the appropriate type.
28094 Otherwise, returns NULL. */
28095
28096static cp_token *
28097cp_parser_require_keyword (cp_parser* parser,
28098 enum rid keyword,
28099 required_token token_desc)
28100{
28101 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28102
28103 if (token && token->keyword != keyword)
28104 {
28105 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28106 return NULL;
28107 }
28108
28109 return token;
28110}
28111
28112/* Returns TRUE iff TOKEN is a token that can begin the body of a
28113 function-definition. */
28114
28115static bool
28116cp_parser_token_starts_function_definition_p (cp_token* token)
28117{
28118 return (/* An ordinary function-body begins with an `{'. */
28119 token->type == CPP_OPEN_BRACE
28120 /* A ctor-initializer begins with a `:'. */
28121 || token->type == CPP_COLON
28122 /* A function-try-block begins with `try'. */
28123 || token->keyword == RID_TRY
28124 /* A function-transaction-block begins with `__transaction_atomic'
28125 or `__transaction_relaxed'. */
28126 || token->keyword == RID_TRANSACTION_ATOMIC
28127 || token->keyword == RID_TRANSACTION_RELAXED
28128 /* The named return value extension begins with `return'. */
28129 || token->keyword == RID_RETURN);
28130}
28131
28132/* Returns TRUE iff the next token is the ":" or "{" beginning a class
28133 definition. */
28134
28135static bool
28136cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28137{
28138 cp_token *token;
28139
28140 token = cp_lexer_peek_token (parser->lexer);
28141 return (token->type == CPP_OPEN_BRACE
28142 || (token->type == CPP_COLON
28143 && !parser->colon_doesnt_start_class_def_p));
28144}
28145
28146/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28147 C++0x) ending a template-argument. */
28148
28149static bool
28150cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28151{
28152 cp_token *token;
28153
28154 token = cp_lexer_peek_token (parser->lexer);
28155 return (token->type == CPP_COMMA
28156 || token->type == CPP_GREATER
28157 || token->type == CPP_ELLIPSIS
28158 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28159}
28160
28161/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28162 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28163
28164static bool
28165cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28166 size_t n)
28167{
28168 cp_token *token;
28169
28170 token = cp_lexer_peek_nth_token (parser->lexer, n);
28171 if (token->type == CPP_LESS)
28172 return true;
28173 /* Check for the sequence `<::' in the original code. It would be lexed as
28174 `[:', where `[' is a digraph, and there is no whitespace before
28175 `:'. */
28176 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28177 {
28178 cp_token *token2;
28179 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28180 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28181 return true;
28182 }
28183 return false;
28184}
28185
28186/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28187 or none_type otherwise. */
28188
28189static enum tag_types
28190cp_parser_token_is_class_key (cp_token* token)
28191{
28192 switch (token->keyword)
28193 {
28194 case RID_CLASS:
28195 return class_type;
28196 case RID_STRUCT:
28197 return record_type;
28198 case RID_UNION:
28199 return union_type;
28200
28201 default:
28202 return none_type;
28203 }
28204}
28205
28206/* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28207 or none_type otherwise or if the token is null. */
28208
28209static enum tag_types
28210cp_parser_token_is_type_parameter_key (cp_token* token)
28211{
28212 if (!token)
28213 return none_type;
28214
28215 switch (token->keyword)
28216 {
28217 case RID_CLASS:
28218 return class_type;
28219 case RID_TYPENAME:
28220 return typename_type;
28221
28222 default:
28223 return none_type;
28224 }
28225}
28226
28227/* Issue an error message if the CLASS_KEY does not match the TYPE. */
28228
28229static void
28230cp_parser_check_class_key (enum tag_types class_key, tree type)
28231{
28232 if (type == error_mark_node)
28233 return;
28234 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28235 {
28236 if (permerror (input_location, "%qs tag used in naming %q#T",
28237 class_key == union_type ? "union"
28238 : class_key == record_type ? "struct" : "class",
28239 type))
28240 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28241 "%q#T was previously declared here", type);
28242 }
28243}
28244
28245/* Issue an error message if DECL is redeclared with different
28246 access than its original declaration [class.access.spec/3].
28247 This applies to nested classes, nested class templates and
28248 enumerations [class.mem/1]. */
28249
28250static void
28251cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28252{
28253 if (!decl
28254 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28255 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28256 return;
28257
28258 if ((TREE_PRIVATE (decl)
28259 != (current_access_specifier == access_private_node))
28260 || (TREE_PROTECTED (decl)
28261 != (current_access_specifier == access_protected_node)))
28262 error_at (location, "%qD redeclared with different access", decl);
28263}
28264
28265/* Look for the `template' keyword, as a syntactic disambiguator.
28266 Return TRUE iff it is present, in which case it will be
28267 consumed. */
28268
28269static bool
28270cp_parser_optional_template_keyword (cp_parser *parser)
28271{
28272 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28273 {
28274 /* In C++98 the `template' keyword can only be used within templates;
28275 outside templates the parser can always figure out what is a
28276 template and what is not. In C++11, per the resolution of DR 468,
28277 `template' is allowed in cases where it is not strictly necessary. */
28278 if (!processing_template_decl
28279 && pedantic && cxx_dialect == cxx98)
28280 {
28281 cp_token *token = cp_lexer_peek_token (parser->lexer);
28282 pedwarn (token->location, OPT_Wpedantic,
28283 "in C++98 %<template%> (as a disambiguator) is only "
28284 "allowed within templates");
28285 /* If this part of the token stream is rescanned, the same
28286 error message would be generated. So, we purge the token
28287 from the stream. */
28288 cp_lexer_purge_token (parser->lexer);
28289 return false;
28290 }
28291 else
28292 {
28293 /* Consume the `template' keyword. */
28294 cp_lexer_consume_token (parser->lexer);
28295 return true;
28296 }
28297 }
28298 return false;
28299}
28300
28301/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28302 set PARSER->SCOPE, and perform other related actions. */
28303
28304static void
28305cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28306{
28307 struct tree_check *check_value;
28308
28309 /* Get the stored value. */
28310 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28311 /* Set the scope from the stored value. */
28312 parser->scope = saved_checks_value (check_value);
28313 parser->qualifying_scope = check_value->qualifying_scope;
28314 parser->object_scope = NULL_TREE;
28315}
28316
28317/* Consume tokens up through a non-nested END token. Returns TRUE if we
28318 encounter the end of a block before what we were looking for. */
28319
28320static bool
28321cp_parser_cache_group (cp_parser *parser,
28322 enum cpp_ttype end,
28323 unsigned depth)
28324{
28325 while (true)
28326 {
28327 cp_token *token = cp_lexer_peek_token (parser->lexer);
28328
28329 /* Abort a parenthesized expression if we encounter a semicolon. */
28330 if ((end == CPP_CLOSE_PAREN || depth == 0)
28331 && token->type == CPP_SEMICOLON)
28332 return true;
28333 /* If we've reached the end of the file, stop. */
28334 if (token->type == CPP_EOF
28335 || (end != CPP_PRAGMA_EOL
28336 && token->type == CPP_PRAGMA_EOL))
28337 return true;
28338 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28339 /* We've hit the end of an enclosing block, so there's been some
28340 kind of syntax error. */
28341 return true;
28342
28343 /* Consume the token. */
28344 cp_lexer_consume_token (parser->lexer);
28345 /* See if it starts a new group. */
28346 if (token->type == CPP_OPEN_BRACE)
28347 {
28348 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28349 /* In theory this should probably check end == '}', but
28350 cp_parser_save_member_function_body needs it to exit
28351 after either '}' or ')' when called with ')'. */
28352 if (depth == 0)
28353 return false;
28354 }
28355 else if (token->type == CPP_OPEN_PAREN)
28356 {
28357 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28358 if (depth == 0 && end == CPP_CLOSE_PAREN)
28359 return false;
28360 }
28361 else if (token->type == CPP_PRAGMA)
28362 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28363 else if (token->type == end)
28364 return false;
28365 }
28366}
28367
28368/* Like above, for caching a default argument or NSDMI. Both of these are
28369 terminated by a non-nested comma, but it can be unclear whether or not a
28370 comma is nested in a template argument list unless we do more parsing.
28371 In order to handle this ambiguity, when we encounter a ',' after a '<'
28372 we try to parse what follows as a parameter-declaration-list (in the
28373 case of a default argument) or a member-declarator (in the case of an
28374 NSDMI). If that succeeds, then we stop caching. */
28375
28376static tree
28377cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28378{
28379 unsigned depth = 0;
28380 int maybe_template_id = 0;
28381 cp_token *first_token;
28382 cp_token *token;
28383 tree default_argument;
28384
28385 /* Add tokens until we have processed the entire default
28386 argument. We add the range [first_token, token). */
28387 first_token = cp_lexer_peek_token (parser->lexer);
28388 if (first_token->type == CPP_OPEN_BRACE)
28389 {
28390 /* For list-initialization, this is straightforward. */
28391 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28392 token = cp_lexer_peek_token (parser->lexer);
28393 }
28394 else while (true)
28395 {
28396 bool done = false;
28397
28398 /* Peek at the next token. */
28399 token = cp_lexer_peek_token (parser->lexer);
28400 /* What we do depends on what token we have. */
28401 switch (token->type)
28402 {
28403 /* In valid code, a default argument must be
28404 immediately followed by a `,' `)', or `...'. */
28405 case CPP_COMMA:
28406 if (depth == 0 && maybe_template_id)
28407 {
28408 /* If we've seen a '<', we might be in a
28409 template-argument-list. Until Core issue 325 is
28410 resolved, we don't know how this situation ought
28411 to be handled, so try to DTRT. We check whether
28412 what comes after the comma is a valid parameter
28413 declaration list. If it is, then the comma ends
28414 the default argument; otherwise the default
28415 argument continues. */
28416 bool error = false;
28417 cp_token *peek;
28418
28419 /* Set ITALP so cp_parser_parameter_declaration_list
28420 doesn't decide to commit to this parse. */
28421 bool saved_italp = parser->in_template_argument_list_p;
28422 parser->in_template_argument_list_p = true;
28423
28424 cp_parser_parse_tentatively (parser);
28425
28426 if (nsdmi)
28427 {
28428 /* Parse declarators until we reach a non-comma or
28429 somthing that cannot be an initializer.
28430 Just checking whether we're looking at a single
28431 declarator is insufficient. Consider:
28432 int var = tuple<T,U>::x;
28433 The template parameter 'U' looks exactly like a
28434 declarator. */
28435 do
28436 {
28437 int ctor_dtor_or_conv_p;
28438 cp_lexer_consume_token (parser->lexer);
28439 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28440 &ctor_dtor_or_conv_p,
28441 /*parenthesized_p=*/NULL,
28442 /*member_p=*/true,
28443 /*friend_p=*/false);
28444 peek = cp_lexer_peek_token (parser->lexer);
28445 if (cp_parser_error_occurred (parser))
28446 break;
28447 }
28448 while (peek->type == CPP_COMMA);
28449 /* If we met an '=' or ';' then the original comma
28450 was the end of the NSDMI. Otherwise assume
28451 we're still in the NSDMI. */
28452 error = (peek->type != CPP_EQ
28453 && peek->type != CPP_SEMICOLON);
28454 }
28455 else
28456 {
28457 cp_lexer_consume_token (parser->lexer);
28458 begin_scope (sk_function_parms, NULL_TREE);
28459 cp_parser_parameter_declaration_list (parser, &error);
28460 pop_bindings_and_leave_scope ();
28461 }
28462 if (!cp_parser_error_occurred (parser) && !error)
28463 done = true;
28464 cp_parser_abort_tentative_parse (parser);
28465
28466 parser->in_template_argument_list_p = saved_italp;
28467 break;
28468 }
28469 /* FALLTHRU */
28470 case CPP_CLOSE_PAREN:
28471 case CPP_ELLIPSIS:
28472 /* If we run into a non-nested `;', `}', or `]',
28473 then the code is invalid -- but the default
28474 argument is certainly over. */
28475 case CPP_SEMICOLON:
28476 case CPP_CLOSE_BRACE:
28477 case CPP_CLOSE_SQUARE:
28478 if (depth == 0
28479 /* Handle correctly int n = sizeof ... ( p ); */
28480 && token->type != CPP_ELLIPSIS)
28481 done = true;
28482 /* Update DEPTH, if necessary. */
28483 else if (token->type == CPP_CLOSE_PAREN
28484 || token->type == CPP_CLOSE_BRACE
28485 || token->type == CPP_CLOSE_SQUARE)
28486 --depth;
28487 break;
28488
28489 case CPP_OPEN_PAREN:
28490 case CPP_OPEN_SQUARE:
28491 case CPP_OPEN_BRACE:
28492 ++depth;
28493 break;
28494
28495 case CPP_LESS:
28496 if (depth == 0)
28497 /* This might be the comparison operator, or it might
28498 start a template argument list. */
28499 ++maybe_template_id;
28500 break;
28501
28502 case CPP_RSHIFT:
28503 if (cxx_dialect == cxx98)
28504 break;
28505 /* Fall through for C++0x, which treats the `>>'
28506 operator like two `>' tokens in certain
28507 cases. */
28508 gcc_fallthrough ();
28509
28510 case CPP_GREATER:
28511 if (depth == 0)
28512 {
28513 /* This might be an operator, or it might close a
28514 template argument list. But if a previous '<'
28515 started a template argument list, this will have
28516 closed it, so we can't be in one anymore. */
28517 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28518 if (maybe_template_id < 0)
28519 maybe_template_id = 0;
28520 }
28521 break;
28522
28523 /* If we run out of tokens, issue an error message. */
28524 case CPP_EOF:
28525 case CPP_PRAGMA_EOL:
28526 error_at (token->location, "file ends in default argument");
28527 return error_mark_node;
28528
28529 case CPP_NAME:
28530 case CPP_SCOPE:
28531 /* In these cases, we should look for template-ids.
28532 For example, if the default argument is
28533 `X<int, double>()', we need to do name lookup to
28534 figure out whether or not `X' is a template; if
28535 so, the `,' does not end the default argument.
28536
28537 That is not yet done. */
28538 break;
28539
28540 default:
28541 break;
28542 }
28543
28544 /* If we've reached the end, stop. */
28545 if (done)
28546 break;
28547
28548 /* Add the token to the token block. */
28549 token = cp_lexer_consume_token (parser->lexer);
28550 }
28551
28552 /* Create a DEFAULT_ARG to represent the unparsed default
28553 argument. */
28554 default_argument = make_node (DEFAULT_ARG);
28555 DEFARG_TOKENS (default_argument)
28556 = cp_token_cache_new (first_token, token);
28557 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28558
28559 return default_argument;
28560}
28561
28562/* Begin parsing tentatively. We always save tokens while parsing
28563 tentatively so that if the tentative parsing fails we can restore the
28564 tokens. */
28565
28566static void
28567cp_parser_parse_tentatively (cp_parser* parser)
28568{
28569 /* Enter a new parsing context. */
28570 parser->context = cp_parser_context_new (parser->context);
28571 /* Begin saving tokens. */
28572 cp_lexer_save_tokens (parser->lexer);
28573 /* In order to avoid repetitive access control error messages,
28574 access checks are queued up until we are no longer parsing
28575 tentatively. */
28576 push_deferring_access_checks (dk_deferred);
28577}
28578
28579/* Commit to the currently active tentative parse. */
28580
28581static void
28582cp_parser_commit_to_tentative_parse (cp_parser* parser)
28583{
28584 cp_parser_context *context;
28585 cp_lexer *lexer;
28586
28587 /* Mark all of the levels as committed. */
28588 lexer = parser->lexer;
28589 for (context = parser->context; context->next; context = context->next)
28590 {
28591 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28592 break;
28593 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28594 while (!cp_lexer_saving_tokens (lexer))
28595 lexer = lexer->next;
28596 cp_lexer_commit_tokens (lexer);
28597 }
28598}
28599
28600/* Commit to the topmost currently active tentative parse.
28601
28602 Note that this function shouldn't be called when there are
28603 irreversible side-effects while in a tentative state. For
28604 example, we shouldn't create a permanent entry in the symbol
28605 table, or issue an error message that might not apply if the
28606 tentative parse is aborted. */
28607
28608static void
28609cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28610{
28611 cp_parser_context *context = parser->context;
28612 cp_lexer *lexer = parser->lexer;
28613
28614 if (context)
28615 {
28616 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28617 return;
28618 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28619
28620 while (!cp_lexer_saving_tokens (lexer))
28621 lexer = lexer->next;
28622 cp_lexer_commit_tokens (lexer);
28623 }
28624}
28625
28626/* Abort the currently active tentative parse. All consumed tokens
28627 will be rolled back, and no diagnostics will be issued. */
28628
28629static void
28630cp_parser_abort_tentative_parse (cp_parser* parser)
28631{
28632 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28633 || errorcount > 0);
28634 cp_parser_simulate_error (parser);
28635 /* Now, pretend that we want to see if the construct was
28636 successfully parsed. */
28637 cp_parser_parse_definitely (parser);
28638}
28639
28640/* Stop parsing tentatively. If a parse error has occurred, restore the
28641 token stream. Otherwise, commit to the tokens we have consumed.
28642 Returns true if no error occurred; false otherwise. */
28643
28644static bool
28645cp_parser_parse_definitely (cp_parser* parser)
28646{
28647 bool error_occurred;
28648 cp_parser_context *context;
28649
28650 /* Remember whether or not an error occurred, since we are about to
28651 destroy that information. */
28652 error_occurred = cp_parser_error_occurred (parser);
28653 /* Remove the topmost context from the stack. */
28654 context = parser->context;
28655 parser->context = context->next;
28656 /* If no parse errors occurred, commit to the tentative parse. */
28657 if (!error_occurred)
28658 {
28659 /* Commit to the tokens read tentatively, unless that was
28660 already done. */
28661 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28662 cp_lexer_commit_tokens (parser->lexer);
28663
28664 pop_to_parent_deferring_access_checks ();
28665 }
28666 /* Otherwise, if errors occurred, roll back our state so that things
28667 are just as they were before we began the tentative parse. */
28668 else
28669 {
28670 cp_lexer_rollback_tokens (parser->lexer);
28671 pop_deferring_access_checks ();
28672 }
28673 /* Add the context to the front of the free list. */
28674 context->next = cp_parser_context_free_list;
28675 cp_parser_context_free_list = context;
28676
28677 return !error_occurred;
28678}
28679
28680/* Returns true if we are parsing tentatively and are not committed to
28681 this tentative parse. */
28682
28683static bool
28684cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28685{
28686 return (cp_parser_parsing_tentatively (parser)
28687 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28688}
28689
28690/* Returns nonzero iff an error has occurred during the most recent
28691 tentative parse. */
28692
28693static bool
28694cp_parser_error_occurred (cp_parser* parser)
28695{
28696 return (cp_parser_parsing_tentatively (parser)
28697 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28698}
28699
28700/* Returns nonzero if GNU extensions are allowed. */
28701
28702static bool
28703cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28704{
28705 return parser->allow_gnu_extensions_p;
28706}
28707
28708/* Objective-C++ Productions */
28709
28710
28711/* Parse an Objective-C expression, which feeds into a primary-expression
28712 above.
28713
28714 objc-expression:
28715 objc-message-expression
28716 objc-string-literal
28717 objc-encode-expression
28718 objc-protocol-expression
28719 objc-selector-expression
28720
28721 Returns a tree representation of the expression. */
28722
28723static cp_expr
28724cp_parser_objc_expression (cp_parser* parser)
28725{
28726 /* Try to figure out what kind of declaration is present. */
28727 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28728
28729 switch (kwd->type)
28730 {
28731 case CPP_OPEN_SQUARE:
28732 return cp_parser_objc_message_expression (parser);
28733
28734 case CPP_OBJC_STRING:
28735 kwd = cp_lexer_consume_token (parser->lexer);
28736 return objc_build_string_object (kwd->u.value);
28737
28738 case CPP_KEYWORD:
28739 switch (kwd->keyword)
28740 {
28741 case RID_AT_ENCODE:
28742 return cp_parser_objc_encode_expression (parser);
28743
28744 case RID_AT_PROTOCOL:
28745 return cp_parser_objc_protocol_expression (parser);
28746
28747 case RID_AT_SELECTOR:
28748 return cp_parser_objc_selector_expression (parser);
28749
28750 default:
28751 break;
28752 }
28753 default:
28754 error_at (kwd->location,
28755 "misplaced %<@%D%> Objective-C++ construct",
28756 kwd->u.value);
28757 cp_parser_skip_to_end_of_block_or_statement (parser);
28758 }
28759
28760 return error_mark_node;
28761}
28762
28763/* Parse an Objective-C message expression.
28764
28765 objc-message-expression:
28766 [ objc-message-receiver objc-message-args ]
28767
28768 Returns a representation of an Objective-C message. */
28769
28770static tree
28771cp_parser_objc_message_expression (cp_parser* parser)
28772{
28773 tree receiver, messageargs;
28774
28775 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28776 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28777 receiver = cp_parser_objc_message_receiver (parser);
28778 messageargs = cp_parser_objc_message_args (parser);
28779 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28780 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28781
28782 tree result = objc_build_message_expr (receiver, messageargs);
28783
28784 /* Construct a location e.g.
28785 [self func1:5]
28786 ^~~~~~~~~~~~~~
28787 ranging from the '[' to the ']', with the caret at the start. */
28788 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28789 protected_set_expr_location (result, combined_loc);
28790
28791 return result;
28792}
28793
28794/* Parse an objc-message-receiver.
28795
28796 objc-message-receiver:
28797 expression
28798 simple-type-specifier
28799
28800 Returns a representation of the type or expression. */
28801
28802static tree
28803cp_parser_objc_message_receiver (cp_parser* parser)
28804{
28805 tree rcv;
28806
28807 /* An Objective-C message receiver may be either (1) a type
28808 or (2) an expression. */
28809 cp_parser_parse_tentatively (parser);
28810 rcv = cp_parser_expression (parser);
28811
28812 /* If that worked out, fine. */
28813 if (cp_parser_parse_definitely (parser))
28814 return rcv;
28815
28816 cp_parser_parse_tentatively (parser);
28817 rcv = cp_parser_simple_type_specifier (parser,
28818 /*decl_specs=*/NULL,
28819 CP_PARSER_FLAGS_NONE);
28820
28821 if (cp_parser_parse_definitely (parser))
28822 return objc_get_class_reference (rcv);
28823
28824 cp_parser_error (parser, "objective-c++ message receiver expected");
28825 return error_mark_node;
28826}
28827
28828/* Parse the arguments and selectors comprising an Objective-C message.
28829
28830 objc-message-args:
28831 objc-selector
28832 objc-selector-args
28833 objc-selector-args , objc-comma-args
28834
28835 objc-selector-args:
28836 objc-selector [opt] : assignment-expression
28837 objc-selector-args objc-selector [opt] : assignment-expression
28838
28839 objc-comma-args:
28840 assignment-expression
28841 objc-comma-args , assignment-expression
28842
28843 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28844 selector arguments and TREE_VALUE containing a list of comma
28845 arguments. */
28846
28847static tree
28848cp_parser_objc_message_args (cp_parser* parser)
28849{
28850 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28851 bool maybe_unary_selector_p = true;
28852 cp_token *token = cp_lexer_peek_token (parser->lexer);
28853
28854 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28855 {
28856 tree selector = NULL_TREE, arg;
28857
28858 if (token->type != CPP_COLON)
28859 selector = cp_parser_objc_selector (parser);
28860
28861 /* Detect if we have a unary selector. */
28862 if (maybe_unary_selector_p
28863 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28864 return build_tree_list (selector, NULL_TREE);
28865
28866 maybe_unary_selector_p = false;
28867 cp_parser_require (parser, CPP_COLON, RT_COLON);
28868 arg = cp_parser_assignment_expression (parser);
28869
28870 sel_args
28871 = chainon (sel_args,
28872 build_tree_list (selector, arg));
28873
28874 token = cp_lexer_peek_token (parser->lexer);
28875 }
28876
28877 /* Handle non-selector arguments, if any. */
28878 while (token->type == CPP_COMMA)
28879 {
28880 tree arg;
28881
28882 cp_lexer_consume_token (parser->lexer);
28883 arg = cp_parser_assignment_expression (parser);
28884
28885 addl_args
28886 = chainon (addl_args,
28887 build_tree_list (NULL_TREE, arg));
28888
28889 token = cp_lexer_peek_token (parser->lexer);
28890 }
28891
28892 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28893 {
28894 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28895 return build_tree_list (error_mark_node, error_mark_node);
28896 }
28897
28898 return build_tree_list (sel_args, addl_args);
28899}
28900
28901/* Parse an Objective-C encode expression.
28902
28903 objc-encode-expression:
28904 @encode objc-typename
28905
28906 Returns an encoded representation of the type argument. */
28907
28908static cp_expr
28909cp_parser_objc_encode_expression (cp_parser* parser)
28910{
28911 tree type;
28912 cp_token *token;
28913 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28914
28915 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28916 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28917 token = cp_lexer_peek_token (parser->lexer);
28918 type = complete_type (cp_parser_type_id (parser));
28919 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28920
28921 if (!type)
28922 {
28923 error_at (token->location,
28924 "%<@encode%> must specify a type as an argument");
28925 return error_mark_node;
28926 }
28927
28928 /* This happens if we find @encode(T) (where T is a template
28929 typename or something dependent on a template typename) when
28930 parsing a template. In that case, we can't compile it
28931 immediately, but we rather create an AT_ENCODE_EXPR which will
28932 need to be instantiated when the template is used.
28933 */
28934 if (dependent_type_p (type))
28935 {
28936 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28937 TREE_READONLY (value) = 1;
28938 return value;
28939 }
28940
28941
28942 /* Build a location of the form:
28943 @encode(int)
28944 ^~~~~~~~~~~~
28945 with caret==start at the @ token, finishing at the close paren. */
28946 location_t combined_loc
28947 = make_location (start_loc, start_loc,
28948 cp_lexer_previous_token (parser->lexer)->location);
28949
28950 return cp_expr (objc_build_encode_expr (type), combined_loc);
28951}
28952
28953/* Parse an Objective-C @defs expression. */
28954
28955static tree
28956cp_parser_objc_defs_expression (cp_parser *parser)
28957{
28958 tree name;
28959
28960 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28961 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28962 name = cp_parser_identifier (parser);
28963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28964
28965 return objc_get_class_ivars (name);
28966}
28967
28968/* Parse an Objective-C protocol expression.
28969
28970 objc-protocol-expression:
28971 @protocol ( identifier )
28972
28973 Returns a representation of the protocol expression. */
28974
28975static tree
28976cp_parser_objc_protocol_expression (cp_parser* parser)
28977{
28978 tree proto;
28979 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28980
28981 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28982 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28983 proto = cp_parser_identifier (parser);
28984 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28985
28986 /* Build a location of the form:
28987 @protocol(prot)
28988 ^~~~~~~~~~~~~~~
28989 with caret==start at the @ token, finishing at the close paren. */
28990 location_t combined_loc
28991 = make_location (start_loc, start_loc,
28992 cp_lexer_previous_token (parser->lexer)->location);
28993 tree result = objc_build_protocol_expr (proto);
28994 protected_set_expr_location (result, combined_loc);
28995 return result;
28996}
28997
28998/* Parse an Objective-C selector expression.
28999
29000 objc-selector-expression:
29001 @selector ( objc-method-signature )
29002
29003 objc-method-signature:
29004 objc-selector
29005 objc-selector-seq
29006
29007 objc-selector-seq:
29008 objc-selector :
29009 objc-selector-seq objc-selector :
29010
29011 Returns a representation of the method selector. */
29012
29013static tree
29014cp_parser_objc_selector_expression (cp_parser* parser)
29015{
29016 tree sel_seq = NULL_TREE;
29017 bool maybe_unary_selector_p = true;
29018 cp_token *token;
29019 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29020
29021 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29022 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29023 token = cp_lexer_peek_token (parser->lexer);
29024
29025 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29026 || token->type == CPP_SCOPE)
29027 {
29028 tree selector = NULL_TREE;
29029
29030 if (token->type != CPP_COLON
29031 || token->type == CPP_SCOPE)
29032 selector = cp_parser_objc_selector (parser);
29033
29034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29035 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29036 {
29037 /* Detect if we have a unary selector. */
29038 if (maybe_unary_selector_p)
29039 {
29040 sel_seq = selector;
29041 goto finish_selector;
29042 }
29043 else
29044 {
29045 cp_parser_error (parser, "expected %<:%>");
29046 }
29047 }
29048 maybe_unary_selector_p = false;
29049 token = cp_lexer_consume_token (parser->lexer);
29050
29051 if (token->type == CPP_SCOPE)
29052 {
29053 sel_seq
29054 = chainon (sel_seq,
29055 build_tree_list (selector, NULL_TREE));
29056 sel_seq
29057 = chainon (sel_seq,
29058 build_tree_list (NULL_TREE, NULL_TREE));
29059 }
29060 else
29061 sel_seq
29062 = chainon (sel_seq,
29063 build_tree_list (selector, NULL_TREE));
29064
29065 token = cp_lexer_peek_token (parser->lexer);
29066 }
29067
29068 finish_selector:
29069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29070
29071
29072 /* Build a location of the form:
29073 @selector(func)
29074 ^~~~~~~~~~~~~~~
29075 with caret==start at the @ token, finishing at the close paren. */
29076 location_t combined_loc
29077 = make_location (loc, loc,
29078 cp_lexer_previous_token (parser->lexer)->location);
29079 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29080 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29081 protected_set_expr_location (result, combined_loc);
29082 return result;
29083}
29084
29085/* Parse a list of identifiers.
29086
29087 objc-identifier-list:
29088 identifier
29089 objc-identifier-list , identifier
29090
29091 Returns a TREE_LIST of identifier nodes. */
29092
29093static tree
29094cp_parser_objc_identifier_list (cp_parser* parser)
29095{
29096 tree identifier;
29097 tree list;
29098 cp_token *sep;
29099
29100 identifier = cp_parser_identifier (parser);
29101 if (identifier == error_mark_node)
29102 return error_mark_node;
29103
29104 list = build_tree_list (NULL_TREE, identifier);
29105 sep = cp_lexer_peek_token (parser->lexer);
29106
29107 while (sep->type == CPP_COMMA)
29108 {
29109 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29110 identifier = cp_parser_identifier (parser);
29111 if (identifier == error_mark_node)
29112 return list;
29113
29114 list = chainon (list, build_tree_list (NULL_TREE,
29115 identifier));
29116 sep = cp_lexer_peek_token (parser->lexer);
29117 }
29118
29119 return list;
29120}
29121
29122/* Parse an Objective-C alias declaration.
29123
29124 objc-alias-declaration:
29125 @compatibility_alias identifier identifier ;
29126
29127 This function registers the alias mapping with the Objective-C front end.
29128 It returns nothing. */
29129
29130static void
29131cp_parser_objc_alias_declaration (cp_parser* parser)
29132{
29133 tree alias, orig;
29134
29135 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29136 alias = cp_parser_identifier (parser);
29137 orig = cp_parser_identifier (parser);
29138 objc_declare_alias (alias, orig);
29139 cp_parser_consume_semicolon_at_end_of_statement (parser);
29140}
29141
29142/* Parse an Objective-C class forward-declaration.
29143
29144 objc-class-declaration:
29145 @class objc-identifier-list ;
29146
29147 The function registers the forward declarations with the Objective-C
29148 front end. It returns nothing. */
29149
29150static void
29151cp_parser_objc_class_declaration (cp_parser* parser)
29152{
29153 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29154 while (true)
29155 {
29156 tree id;
29157
29158 id = cp_parser_identifier (parser);
29159 if (id == error_mark_node)
29160 break;
29161
29162 objc_declare_class (id);
29163
29164 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29165 cp_lexer_consume_token (parser->lexer);
29166 else
29167 break;
29168 }
29169 cp_parser_consume_semicolon_at_end_of_statement (parser);
29170}
29171
29172/* Parse a list of Objective-C protocol references.
29173
29174 objc-protocol-refs-opt:
29175 objc-protocol-refs [opt]
29176
29177 objc-protocol-refs:
29178 < objc-identifier-list >
29179
29180 Returns a TREE_LIST of identifiers, if any. */
29181
29182static tree
29183cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29184{
29185 tree protorefs = NULL_TREE;
29186
29187 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29188 {
29189 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29190 protorefs = cp_parser_objc_identifier_list (parser);
29191 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29192 }
29193
29194 return protorefs;
29195}
29196
29197/* Parse a Objective-C visibility specification. */
29198
29199static void
29200cp_parser_objc_visibility_spec (cp_parser* parser)
29201{
29202 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29203
29204 switch (vis->keyword)
29205 {
29206 case RID_AT_PRIVATE:
29207 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29208 break;
29209 case RID_AT_PROTECTED:
29210 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29211 break;
29212 case RID_AT_PUBLIC:
29213 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29214 break;
29215 case RID_AT_PACKAGE:
29216 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29217 break;
29218 default:
29219 return;
29220 }
29221
29222 /* Eat '@private'/'@protected'/'@public'. */
29223 cp_lexer_consume_token (parser->lexer);
29224}
29225
29226/* Parse an Objective-C method type. Return 'true' if it is a class
29227 (+) method, and 'false' if it is an instance (-) method. */
29228
29229static inline bool
29230cp_parser_objc_method_type (cp_parser* parser)
29231{
29232 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29233 return true;
29234 else
29235 return false;
29236}
29237
29238/* Parse an Objective-C protocol qualifier. */
29239
29240static tree
29241cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29242{
29243 tree quals = NULL_TREE, node;
29244 cp_token *token = cp_lexer_peek_token (parser->lexer);
29245
29246 node = token->u.value;
29247
29248 while (node && identifier_p (node)
29249 && (node == ridpointers [(int) RID_IN]
29250 || node == ridpointers [(int) RID_OUT]
29251 || node == ridpointers [(int) RID_INOUT]
29252 || node == ridpointers [(int) RID_BYCOPY]
29253 || node == ridpointers [(int) RID_BYREF]
29254 || node == ridpointers [(int) RID_ONEWAY]))
29255 {
29256 quals = tree_cons (NULL_TREE, node, quals);
29257 cp_lexer_consume_token (parser->lexer);
29258 token = cp_lexer_peek_token (parser->lexer);
29259 node = token->u.value;
29260 }
29261
29262 return quals;
29263}
29264
29265/* Parse an Objective-C typename. */
29266
29267static tree
29268cp_parser_objc_typename (cp_parser* parser)
29269{
29270 tree type_name = NULL_TREE;
29271
29272 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29273 {
29274 tree proto_quals, cp_type = NULL_TREE;
29275
29276 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29277 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29278
29279 /* An ObjC type name may consist of just protocol qualifiers, in which
29280 case the type shall default to 'id'. */
29281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29282 {
29283 cp_type = cp_parser_type_id (parser);
29284
29285 /* If the type could not be parsed, an error has already
29286 been produced. For error recovery, behave as if it had
29287 not been specified, which will use the default type
29288 'id'. */
29289 if (cp_type == error_mark_node)
29290 {
29291 cp_type = NULL_TREE;
29292 /* We need to skip to the closing parenthesis as
29293 cp_parser_type_id() does not seem to do it for
29294 us. */
29295 cp_parser_skip_to_closing_parenthesis (parser,
29296 /*recovering=*/true,
29297 /*or_comma=*/false,
29298 /*consume_paren=*/false);
29299 }
29300 }
29301
29302 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29303 type_name = build_tree_list (proto_quals, cp_type);
29304 }
29305
29306 return type_name;
29307}
29308
29309/* Check to see if TYPE refers to an Objective-C selector name. */
29310
29311static bool
29312cp_parser_objc_selector_p (enum cpp_ttype type)
29313{
29314 return (type == CPP_NAME || type == CPP_KEYWORD
29315 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29316 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29317 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29318 || type == CPP_XOR || type == CPP_XOR_EQ);
29319}
29320
29321/* Parse an Objective-C selector. */
29322
29323static tree
29324cp_parser_objc_selector (cp_parser* parser)
29325{
29326 cp_token *token = cp_lexer_consume_token (parser->lexer);
29327
29328 if (!cp_parser_objc_selector_p (token->type))
29329 {
29330 error_at (token->location, "invalid Objective-C++ selector name");
29331 return error_mark_node;
29332 }
29333
29334 /* C++ operator names are allowed to appear in ObjC selectors. */
29335 switch (token->type)
29336 {
29337 case CPP_AND_AND: return get_identifier ("and");
29338 case CPP_AND_EQ: return get_identifier ("and_eq");
29339 case CPP_AND: return get_identifier ("bitand");
29340 case CPP_OR: return get_identifier ("bitor");
29341 case CPP_COMPL: return get_identifier ("compl");
29342 case CPP_NOT: return get_identifier ("not");
29343 case CPP_NOT_EQ: return get_identifier ("not_eq");
29344 case CPP_OR_OR: return get_identifier ("or");
29345 case CPP_OR_EQ: return get_identifier ("or_eq");
29346 case CPP_XOR: return get_identifier ("xor");
29347 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29348 default: return token->u.value;
29349 }
29350}
29351
29352/* Parse an Objective-C params list. */
29353
29354static tree
29355cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29356{
29357 tree params = NULL_TREE;
29358 bool maybe_unary_selector_p = true;
29359 cp_token *token = cp_lexer_peek_token (parser->lexer);
29360
29361 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29362 {
29363 tree selector = NULL_TREE, type_name, identifier;
29364 tree parm_attr = NULL_TREE;
29365
29366 if (token->keyword == RID_ATTRIBUTE)
29367 break;
29368
29369 if (token->type != CPP_COLON)
29370 selector = cp_parser_objc_selector (parser);
29371
29372 /* Detect if we have a unary selector. */
29373 if (maybe_unary_selector_p
29374 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29375 {
29376 params = selector; /* Might be followed by attributes. */
29377 break;
29378 }
29379
29380 maybe_unary_selector_p = false;
29381 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29382 {
29383 /* Something went quite wrong. There should be a colon
29384 here, but there is not. Stop parsing parameters. */
29385 break;
29386 }
29387 type_name = cp_parser_objc_typename (parser);
29388 /* New ObjC allows attributes on parameters too. */
29389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29390 parm_attr = cp_parser_attributes_opt (parser);
29391 identifier = cp_parser_identifier (parser);
29392
29393 params
29394 = chainon (params,
29395 objc_build_keyword_decl (selector,
29396 type_name,
29397 identifier,
29398 parm_attr));
29399
29400 token = cp_lexer_peek_token (parser->lexer);
29401 }
29402
29403 if (params == NULL_TREE)
29404 {
29405 cp_parser_error (parser, "objective-c++ method declaration is expected");
29406 return error_mark_node;
29407 }
29408
29409 /* We allow tail attributes for the method. */
29410 if (token->keyword == RID_ATTRIBUTE)
29411 {
29412 *attributes = cp_parser_attributes_opt (parser);
29413 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29414 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29415 return params;
29416 cp_parser_error (parser,
29417 "method attributes must be specified at the end");
29418 return error_mark_node;
29419 }
29420
29421 if (params == NULL_TREE)
29422 {
29423 cp_parser_error (parser, "objective-c++ method declaration is expected");
29424 return error_mark_node;
29425 }
29426 return params;
29427}
29428
29429/* Parse the non-keyword Objective-C params. */
29430
29431static tree
29432cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29433 tree* attributes)
29434{
29435 tree params = make_node (TREE_LIST);
29436 cp_token *token = cp_lexer_peek_token (parser->lexer);
29437 *ellipsisp = false; /* Initially, assume no ellipsis. */
29438
29439 while (token->type == CPP_COMMA)
29440 {
29441 cp_parameter_declarator *parmdecl;
29442 tree parm;
29443
29444 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29445 token = cp_lexer_peek_token (parser->lexer);
29446
29447 if (token->type == CPP_ELLIPSIS)
29448 {
29449 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29450 *ellipsisp = true;
29451 token = cp_lexer_peek_token (parser->lexer);
29452 break;
29453 }
29454
29455 /* TODO: parse attributes for tail parameters. */
29456 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29457 parm = grokdeclarator (parmdecl->declarator,
29458 &parmdecl->decl_specifiers,
29459 PARM, /*initialized=*/0,
29460 /*attrlist=*/NULL);
29461
29462 chainon (params, build_tree_list (NULL_TREE, parm));
29463 token = cp_lexer_peek_token (parser->lexer);
29464 }
29465
29466 /* We allow tail attributes for the method. */
29467 if (token->keyword == RID_ATTRIBUTE)
29468 {
29469 if (*attributes == NULL_TREE)
29470 {
29471 *attributes = cp_parser_attributes_opt (parser);
29472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29473 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29474 return params;
29475 }
29476 else
29477 /* We have an error, but parse the attributes, so that we can
29478 carry on. */
29479 *attributes = cp_parser_attributes_opt (parser);
29480
29481 cp_parser_error (parser,
29482 "method attributes must be specified at the end");
29483 return error_mark_node;
29484 }
29485
29486 return params;
29487}
29488
29489/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29490
29491static void
29492cp_parser_objc_interstitial_code (cp_parser* parser)
29493{
29494 cp_token *token = cp_lexer_peek_token (parser->lexer);
29495
29496 /* If the next token is `extern' and the following token is a string
29497 literal, then we have a linkage specification. */
29498 if (token->keyword == RID_EXTERN
29499 && cp_parser_is_pure_string_literal
29500 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29501 cp_parser_linkage_specification (parser);
29502 /* Handle #pragma, if any. */
29503 else if (token->type == CPP_PRAGMA)
29504 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29505 /* Allow stray semicolons. */
29506 else if (token->type == CPP_SEMICOLON)
29507 cp_lexer_consume_token (parser->lexer);
29508 /* Mark methods as optional or required, when building protocols. */
29509 else if (token->keyword == RID_AT_OPTIONAL)
29510 {
29511 cp_lexer_consume_token (parser->lexer);
29512 objc_set_method_opt (true);
29513 }
29514 else if (token->keyword == RID_AT_REQUIRED)
29515 {
29516 cp_lexer_consume_token (parser->lexer);
29517 objc_set_method_opt (false);
29518 }
29519 else if (token->keyword == RID_NAMESPACE)
29520 cp_parser_namespace_definition (parser);
29521 /* Other stray characters must generate errors. */
29522 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29523 {
29524 cp_lexer_consume_token (parser->lexer);
29525 error ("stray %qs between Objective-C++ methods",
29526 token->type == CPP_OPEN_BRACE ? "{" : "}");
29527 }
29528 /* Finally, try to parse a block-declaration, or a function-definition. */
29529 else
29530 cp_parser_block_declaration (parser, /*statement_p=*/false);
29531}
29532
29533/* Parse a method signature. */
29534
29535static tree
29536cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29537{
29538 tree rettype, kwdparms, optparms;
29539 bool ellipsis = false;
29540 bool is_class_method;
29541
29542 is_class_method = cp_parser_objc_method_type (parser);
29543 rettype = cp_parser_objc_typename (parser);
29544 *attributes = NULL_TREE;
29545 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29546 if (kwdparms == error_mark_node)
29547 return error_mark_node;
29548 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29549 if (optparms == error_mark_node)
29550 return error_mark_node;
29551
29552 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29553}
29554
29555static bool
29556cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29557{
29558 tree tattr;
29559 cp_lexer_save_tokens (parser->lexer);
29560 tattr = cp_parser_attributes_opt (parser);
29561 gcc_assert (tattr) ;
29562
29563 /* If the attributes are followed by a method introducer, this is not allowed.
29564 Dump the attributes and flag the situation. */
29565 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29566 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29567 return true;
29568
29569 /* Otherwise, the attributes introduce some interstitial code, possibly so
29570 rewind to allow that check. */
29571 cp_lexer_rollback_tokens (parser->lexer);
29572 return false;
29573}
29574
29575/* Parse an Objective-C method prototype list. */
29576
29577static void
29578cp_parser_objc_method_prototype_list (cp_parser* parser)
29579{
29580 cp_token *token = cp_lexer_peek_token (parser->lexer);
29581
29582 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29583 {
29584 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29585 {
29586 tree attributes, sig;
29587 bool is_class_method;
29588 if (token->type == CPP_PLUS)
29589 is_class_method = true;
29590 else
29591 is_class_method = false;
29592 sig = cp_parser_objc_method_signature (parser, &attributes);
29593 if (sig == error_mark_node)
29594 {
29595 cp_parser_skip_to_end_of_block_or_statement (parser);
29596 token = cp_lexer_peek_token (parser->lexer);
29597 continue;
29598 }
29599 objc_add_method_declaration (is_class_method, sig, attributes);
29600 cp_parser_consume_semicolon_at_end_of_statement (parser);
29601 }
29602 else if (token->keyword == RID_AT_PROPERTY)
29603 cp_parser_objc_at_property_declaration (parser);
29604 else if (token->keyword == RID_ATTRIBUTE
29605 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29606 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29607 OPT_Wattributes,
29608 "prefix attributes are ignored for methods");
29609 else
29610 /* Allow for interspersed non-ObjC++ code. */
29611 cp_parser_objc_interstitial_code (parser);
29612
29613 token = cp_lexer_peek_token (parser->lexer);
29614 }
29615
29616 if (token->type != CPP_EOF)
29617 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29618 else
29619 cp_parser_error (parser, "expected %<@end%>");
29620
29621 objc_finish_interface ();
29622}
29623
29624/* Parse an Objective-C method definition list. */
29625
29626static void
29627cp_parser_objc_method_definition_list (cp_parser* parser)
29628{
29629 cp_token *token = cp_lexer_peek_token (parser->lexer);
29630
29631 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29632 {
29633 tree meth;
29634
29635 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29636 {
29637 cp_token *ptk;
29638 tree sig, attribute;
29639 bool is_class_method;
29640 if (token->type == CPP_PLUS)
29641 is_class_method = true;
29642 else
29643 is_class_method = false;
29644 push_deferring_access_checks (dk_deferred);
29645 sig = cp_parser_objc_method_signature (parser, &attribute);
29646 if (sig == error_mark_node)
29647 {
29648 cp_parser_skip_to_end_of_block_or_statement (parser);
29649 token = cp_lexer_peek_token (parser->lexer);
29650 continue;
29651 }
29652 objc_start_method_definition (is_class_method, sig, attribute,
29653 NULL_TREE);
29654
29655 /* For historical reasons, we accept an optional semicolon. */
29656 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29657 cp_lexer_consume_token (parser->lexer);
29658
29659 ptk = cp_lexer_peek_token (parser->lexer);
29660 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29661 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29662 {
29663 perform_deferred_access_checks (tf_warning_or_error);
29664 stop_deferring_access_checks ();
29665 meth = cp_parser_function_definition_after_declarator (parser,
29666 false);
29667 pop_deferring_access_checks ();
29668 objc_finish_method_definition (meth);
29669 }
29670 }
29671 /* The following case will be removed once @synthesize is
29672 completely implemented. */
29673 else if (token->keyword == RID_AT_PROPERTY)
29674 cp_parser_objc_at_property_declaration (parser);
29675 else if (token->keyword == RID_AT_SYNTHESIZE)
29676 cp_parser_objc_at_synthesize_declaration (parser);
29677 else if (token->keyword == RID_AT_DYNAMIC)
29678 cp_parser_objc_at_dynamic_declaration (parser);
29679 else if (token->keyword == RID_ATTRIBUTE
29680 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29681 warning_at (token->location, OPT_Wattributes,
29682 "prefix attributes are ignored for methods");
29683 else
29684 /* Allow for interspersed non-ObjC++ code. */
29685 cp_parser_objc_interstitial_code (parser);
29686
29687 token = cp_lexer_peek_token (parser->lexer);
29688 }
29689
29690 if (token->type != CPP_EOF)
29691 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29692 else
29693 cp_parser_error (parser, "expected %<@end%>");
29694
29695 objc_finish_implementation ();
29696}
29697
29698/* Parse Objective-C ivars. */
29699
29700static void
29701cp_parser_objc_class_ivars (cp_parser* parser)
29702{
29703 cp_token *token = cp_lexer_peek_token (parser->lexer);
29704
29705 if (token->type != CPP_OPEN_BRACE)
29706 return; /* No ivars specified. */
29707
29708 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29709 token = cp_lexer_peek_token (parser->lexer);
29710
29711 while (token->type != CPP_CLOSE_BRACE
29712 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29713 {
29714 cp_decl_specifier_seq declspecs;
29715 int decl_class_or_enum_p;
29716 tree prefix_attributes;
29717
29718 cp_parser_objc_visibility_spec (parser);
29719
29720 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29721 break;
29722
29723 cp_parser_decl_specifier_seq (parser,
29724 CP_PARSER_FLAGS_OPTIONAL,
29725 &declspecs,
29726 &decl_class_or_enum_p);
29727
29728 /* auto, register, static, extern, mutable. */
29729 if (declspecs.storage_class != sc_none)
29730 {
29731 cp_parser_error (parser, "invalid type for instance variable");
29732 declspecs.storage_class = sc_none;
29733 }
29734
29735 /* thread_local. */
29736 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29737 {
29738 cp_parser_error (parser, "invalid type for instance variable");
29739 declspecs.locations[ds_thread] = 0;
29740 }
29741
29742 /* typedef. */
29743 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29744 {
29745 cp_parser_error (parser, "invalid type for instance variable");
29746 declspecs.locations[ds_typedef] = 0;
29747 }
29748
29749 prefix_attributes = declspecs.attributes;
29750 declspecs.attributes = NULL_TREE;
29751
29752 /* Keep going until we hit the `;' at the end of the
29753 declaration. */
29754 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29755 {
29756 tree width = NULL_TREE, attributes, first_attribute, decl;
29757 cp_declarator *declarator = NULL;
29758 int ctor_dtor_or_conv_p;
29759
29760 /* Check for a (possibly unnamed) bitfield declaration. */
29761 token = cp_lexer_peek_token (parser->lexer);
29762 if (token->type == CPP_COLON)
29763 goto eat_colon;
29764
29765 if (token->type == CPP_NAME
29766 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29767 == CPP_COLON))
29768 {
29769 /* Get the name of the bitfield. */
29770 declarator = make_id_declarator (NULL_TREE,
29771 cp_parser_identifier (parser),
29772 sfk_none);
29773
29774 eat_colon:
29775 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29776 /* Get the width of the bitfield. */
29777 width
29778 = cp_parser_constant_expression (parser);
29779 }
29780 else
29781 {
29782 /* Parse the declarator. */
29783 declarator
29784 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29785 &ctor_dtor_or_conv_p,
29786 /*parenthesized_p=*/NULL,
29787 /*member_p=*/false,
29788 /*friend_p=*/false);
29789 }
29790
29791 /* Look for attributes that apply to the ivar. */
29792 attributes = cp_parser_attributes_opt (parser);
29793 /* Remember which attributes are prefix attributes and
29794 which are not. */
29795 first_attribute = attributes;
29796 /* Combine the attributes. */
29797 attributes = attr_chainon (prefix_attributes, attributes);
29798
29799 if (width)
29800 /* Create the bitfield declaration. */
29801 decl = grokbitfield (declarator, &declspecs,
29802 width,
29803 attributes);
29804 else
29805 decl = grokfield (declarator, &declspecs,
29806 NULL_TREE, /*init_const_expr_p=*/false,
29807 NULL_TREE, attributes);
29808
29809 /* Add the instance variable. */
29810 if (decl != error_mark_node && decl != NULL_TREE)
29811 objc_add_instance_variable (decl);
29812
29813 /* Reset PREFIX_ATTRIBUTES. */
29814 if (attributes != error_mark_node)
29815 {
29816 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29817 attributes = TREE_CHAIN (attributes);
29818 if (attributes)
29819 TREE_CHAIN (attributes) = NULL_TREE;
29820 }
29821
29822 token = cp_lexer_peek_token (parser->lexer);
29823
29824 if (token->type == CPP_COMMA)
29825 {
29826 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29827 continue;
29828 }
29829 break;
29830 }
29831
29832 cp_parser_consume_semicolon_at_end_of_statement (parser);
29833 token = cp_lexer_peek_token (parser->lexer);
29834 }
29835
29836 if (token->keyword == RID_AT_END)
29837 cp_parser_error (parser, "expected %<}%>");
29838
29839 /* Do not consume the RID_AT_END, so it will be read again as terminating
29840 the @interface of @implementation. */
29841 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29842 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29843
29844 /* For historical reasons, we accept an optional semicolon. */
29845 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29846 cp_lexer_consume_token (parser->lexer);
29847}
29848
29849/* Parse an Objective-C protocol declaration. */
29850
29851static void
29852cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29853{
29854 tree proto, protorefs;
29855 cp_token *tok;
29856
29857 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29859 {
29860 tok = cp_lexer_peek_token (parser->lexer);
29861 error_at (tok->location, "identifier expected after %<@protocol%>");
29862 cp_parser_consume_semicolon_at_end_of_statement (parser);
29863 return;
29864 }
29865
29866 /* See if we have a forward declaration or a definition. */
29867 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29868
29869 /* Try a forward declaration first. */
29870 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29871 {
29872 while (true)
29873 {
29874 tree id;
29875
29876 id = cp_parser_identifier (parser);
29877 if (id == error_mark_node)
29878 break;
29879
29880 objc_declare_protocol (id, attributes);
29881
29882 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29883 cp_lexer_consume_token (parser->lexer);
29884 else
29885 break;
29886 }
29887 cp_parser_consume_semicolon_at_end_of_statement (parser);
29888 }
29889
29890 /* Ok, we got a full-fledged definition (or at least should). */
29891 else
29892 {
29893 proto = cp_parser_identifier (parser);
29894 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29895 objc_start_protocol (proto, protorefs, attributes);
29896 cp_parser_objc_method_prototype_list (parser);
29897 }
29898}
29899
29900/* Parse an Objective-C superclass or category. */
29901
29902static void
29903cp_parser_objc_superclass_or_category (cp_parser *parser,
29904 bool iface_p,
29905 tree *super,
29906 tree *categ, bool *is_class_extension)
29907{
29908 cp_token *next = cp_lexer_peek_token (parser->lexer);
29909
29910 *super = *categ = NULL_TREE;
29911 *is_class_extension = false;
29912 if (next->type == CPP_COLON)
29913 {
29914 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29915 *super = cp_parser_identifier (parser);
29916 }
29917 else if (next->type == CPP_OPEN_PAREN)
29918 {
29919 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29920
29921 /* If there is no category name, and this is an @interface, we
29922 have a class extension. */
29923 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29924 {
29925 *categ = NULL_TREE;
29926 *is_class_extension = true;
29927 }
29928 else
29929 *categ = cp_parser_identifier (parser);
29930
29931 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29932 }
29933}
29934
29935/* Parse an Objective-C class interface. */
29936
29937static void
29938cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29939{
29940 tree name, super, categ, protos;
29941 bool is_class_extension;
29942
29943 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29944 name = cp_parser_identifier (parser);
29945 if (name == error_mark_node)
29946 {
29947 /* It's hard to recover because even if valid @interface stuff
29948 is to follow, we can't compile it (or validate it) if we
29949 don't even know which class it refers to. Let's assume this
29950 was a stray '@interface' token in the stream and skip it.
29951 */
29952 return;
29953 }
29954 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29955 &is_class_extension);
29956 protos = cp_parser_objc_protocol_refs_opt (parser);
29957
29958 /* We have either a class or a category on our hands. */
29959 if (categ || is_class_extension)
29960 objc_start_category_interface (name, categ, protos, attributes);
29961 else
29962 {
29963 objc_start_class_interface (name, super, protos, attributes);
29964 /* Handle instance variable declarations, if any. */
29965 cp_parser_objc_class_ivars (parser);
29966 objc_continue_interface ();
29967 }
29968
29969 cp_parser_objc_method_prototype_list (parser);
29970}
29971
29972/* Parse an Objective-C class implementation. */
29973
29974static void
29975cp_parser_objc_class_implementation (cp_parser* parser)
29976{
29977 tree name, super, categ;
29978 bool is_class_extension;
29979
29980 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29981 name = cp_parser_identifier (parser);
29982 if (name == error_mark_node)
29983 {
29984 /* It's hard to recover because even if valid @implementation
29985 stuff is to follow, we can't compile it (or validate it) if
29986 we don't even know which class it refers to. Let's assume
29987 this was a stray '@implementation' token in the stream and
29988 skip it.
29989 */
29990 return;
29991 }
29992 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29993 &is_class_extension);
29994
29995 /* We have either a class or a category on our hands. */
29996 if (categ)
29997 objc_start_category_implementation (name, categ);
29998 else
29999 {
30000 objc_start_class_implementation (name, super);
30001 /* Handle instance variable declarations, if any. */
30002 cp_parser_objc_class_ivars (parser);
30003 objc_continue_implementation ();
30004 }
30005
30006 cp_parser_objc_method_definition_list (parser);
30007}
30008
30009/* Consume the @end token and finish off the implementation. */
30010
30011static void
30012cp_parser_objc_end_implementation (cp_parser* parser)
30013{
30014 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30015 objc_finish_implementation ();
30016}
30017
30018/* Parse an Objective-C declaration. */
30019
30020static void
30021cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30022{
30023 /* Try to figure out what kind of declaration is present. */
30024 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30025
30026 if (attributes)
30027 switch (kwd->keyword)
30028 {
30029 case RID_AT_ALIAS:
30030 case RID_AT_CLASS:
30031 case RID_AT_END:
30032 error_at (kwd->location, "attributes may not be specified before"
30033 " the %<@%D%> Objective-C++ keyword",
30034 kwd->u.value);
30035 attributes = NULL;
30036 break;
30037 case RID_AT_IMPLEMENTATION:
30038 warning_at (kwd->location, OPT_Wattributes,
30039 "prefix attributes are ignored before %<@%D%>",
30040 kwd->u.value);
30041 attributes = NULL;
30042 default:
30043 break;
30044 }
30045
30046 switch (kwd->keyword)
30047 {
30048 case RID_AT_ALIAS:
30049 cp_parser_objc_alias_declaration (parser);
30050 break;
30051 case RID_AT_CLASS:
30052 cp_parser_objc_class_declaration (parser);
30053 break;
30054 case RID_AT_PROTOCOL:
30055 cp_parser_objc_protocol_declaration (parser, attributes);
30056 break;
30057 case RID_AT_INTERFACE:
30058 cp_parser_objc_class_interface (parser, attributes);
30059 break;
30060 case RID_AT_IMPLEMENTATION:
30061 cp_parser_objc_class_implementation (parser);
30062 break;
30063 case RID_AT_END:
30064 cp_parser_objc_end_implementation (parser);
30065 break;
30066 default:
30067 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30068 kwd->u.value);
30069 cp_parser_skip_to_end_of_block_or_statement (parser);
30070 }
30071}
30072
30073/* Parse an Objective-C try-catch-finally statement.
30074
30075 objc-try-catch-finally-stmt:
30076 @try compound-statement objc-catch-clause-seq [opt]
30077 objc-finally-clause [opt]
30078
30079 objc-catch-clause-seq:
30080 objc-catch-clause objc-catch-clause-seq [opt]
30081
30082 objc-catch-clause:
30083 @catch ( objc-exception-declaration ) compound-statement
30084
30085 objc-finally-clause:
30086 @finally compound-statement
30087
30088 objc-exception-declaration:
30089 parameter-declaration
30090 '...'
30091
30092 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30093
30094 Returns NULL_TREE.
30095
30096 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30097 for C. Keep them in sync. */
30098
30099static tree
30100cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30101{
30102 location_t location;
30103 tree stmt;
30104
30105 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30106 location = cp_lexer_peek_token (parser->lexer)->location;
30107 objc_maybe_warn_exceptions (location);
30108 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30109 node, lest it get absorbed into the surrounding block. */
30110 stmt = push_stmt_list ();
30111 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30112 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30113
30114 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30115 {
30116 cp_parameter_declarator *parm;
30117 tree parameter_declaration = error_mark_node;
30118 bool seen_open_paren = false;
30119
30120 cp_lexer_consume_token (parser->lexer);
30121 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30122 seen_open_paren = true;
30123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30124 {
30125 /* We have "@catch (...)" (where the '...' are literally
30126 what is in the code). Skip the '...'.
30127 parameter_declaration is set to NULL_TREE, and
30128 objc_being_catch_clauses() knows that that means
30129 '...'. */
30130 cp_lexer_consume_token (parser->lexer);
30131 parameter_declaration = NULL_TREE;
30132 }
30133 else
30134 {
30135 /* We have "@catch (NSException *exception)" or something
30136 like that. Parse the parameter declaration. */
30137 parm = cp_parser_parameter_declaration (parser, false, NULL);
30138 if (parm == NULL)
30139 parameter_declaration = error_mark_node;
30140 else
30141 parameter_declaration = grokdeclarator (parm->declarator,
30142 &parm->decl_specifiers,
30143 PARM, /*initialized=*/0,
30144 /*attrlist=*/NULL);
30145 }
30146 if (seen_open_paren)
30147 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30148 else
30149 {
30150 /* If there was no open parenthesis, we are recovering from
30151 an error, and we are trying to figure out what mistake
30152 the user has made. */
30153
30154 /* If there is an immediate closing parenthesis, the user
30155 probably forgot the opening one (ie, they typed "@catch
30156 NSException *e)". Parse the closing parenthesis and keep
30157 going. */
30158 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30159 cp_lexer_consume_token (parser->lexer);
30160
30161 /* If these is no immediate closing parenthesis, the user
30162 probably doesn't know that parenthesis are required at
30163 all (ie, they typed "@catch NSException *e"). So, just
30164 forget about the closing parenthesis and keep going. */
30165 }
30166 objc_begin_catch_clause (parameter_declaration);
30167 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30168 objc_finish_catch_clause ();
30169 }
30170 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30171 {
30172 cp_lexer_consume_token (parser->lexer);
30173 location = cp_lexer_peek_token (parser->lexer)->location;
30174 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30175 node, lest it get absorbed into the surrounding block. */
30176 stmt = push_stmt_list ();
30177 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30178 objc_build_finally_clause (location, pop_stmt_list (stmt));
30179 }
30180
30181 return objc_finish_try_stmt ();
30182}
30183
30184/* Parse an Objective-C synchronized statement.
30185
30186 objc-synchronized-stmt:
30187 @synchronized ( expression ) compound-statement
30188
30189 Returns NULL_TREE. */
30190
30191static tree
30192cp_parser_objc_synchronized_statement (cp_parser *parser)
30193{
30194 location_t location;
30195 tree lock, stmt;
30196
30197 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30198
30199 location = cp_lexer_peek_token (parser->lexer)->location;
30200 objc_maybe_warn_exceptions (location);
30201 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30202 lock = cp_parser_expression (parser);
30203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30204
30205 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30206 node, lest it get absorbed into the surrounding block. */
30207 stmt = push_stmt_list ();
30208 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30209
30210 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30211}
30212
30213/* Parse an Objective-C throw statement.
30214
30215 objc-throw-stmt:
30216 @throw assignment-expression [opt] ;
30217
30218 Returns a constructed '@throw' statement. */
30219
30220static tree
30221cp_parser_objc_throw_statement (cp_parser *parser)
30222{
30223 tree expr = NULL_TREE;
30224 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30225
30226 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30227
30228 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30229 expr = cp_parser_expression (parser);
30230
30231 cp_parser_consume_semicolon_at_end_of_statement (parser);
30232
30233 return objc_build_throw_stmt (loc, expr);
30234}
30235
30236/* Parse an Objective-C statement. */
30237
30238static tree
30239cp_parser_objc_statement (cp_parser * parser)
30240{
30241 /* Try to figure out what kind of declaration is present. */
30242 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30243
30244 switch (kwd->keyword)
30245 {
30246 case RID_AT_TRY:
30247 return cp_parser_objc_try_catch_finally_statement (parser);
30248 case RID_AT_SYNCHRONIZED:
30249 return cp_parser_objc_synchronized_statement (parser);
30250 case RID_AT_THROW:
30251 return cp_parser_objc_throw_statement (parser);
30252 default:
30253 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30254 kwd->u.value);
30255 cp_parser_skip_to_end_of_block_or_statement (parser);
30256 }
30257
30258 return error_mark_node;
30259}
30260
30261/* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30262 look ahead to see if an objc keyword follows the attributes. This
30263 is to detect the use of prefix attributes on ObjC @interface and
30264 @protocol. */
30265
30266static bool
30267cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30268{
30269 cp_lexer_save_tokens (parser->lexer);
30270 *attrib = cp_parser_attributes_opt (parser);
30271 gcc_assert (*attrib);
30272 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30273 {
30274 cp_lexer_commit_tokens (parser->lexer);
30275 return true;
30276 }
30277 cp_lexer_rollback_tokens (parser->lexer);
30278 return false;
30279}
30280
30281/* This routine is a minimal replacement for
30282 c_parser_struct_declaration () used when parsing the list of
30283 types/names or ObjC++ properties. For example, when parsing the
30284 code
30285
30286 @property (readonly) int a, b, c;
30287
30288 this function is responsible for parsing "int a, int b, int c" and
30289 returning the declarations as CHAIN of DECLs.
30290
30291 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30292 similar parsing. */
30293static tree
30294cp_parser_objc_struct_declaration (cp_parser *parser)
30295{
30296 tree decls = NULL_TREE;
30297 cp_decl_specifier_seq declspecs;
30298 int decl_class_or_enum_p;
30299 tree prefix_attributes;
30300
30301 cp_parser_decl_specifier_seq (parser,
30302 CP_PARSER_FLAGS_NONE,
30303 &declspecs,
30304 &decl_class_or_enum_p);
30305
30306 if (declspecs.type == error_mark_node)
30307 return error_mark_node;
30308
30309 /* auto, register, static, extern, mutable. */
30310 if (declspecs.storage_class != sc_none)
30311 {
30312 cp_parser_error (parser, "invalid type for property");
30313 declspecs.storage_class = sc_none;
30314 }
30315
30316 /* thread_local. */
30317 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30318 {
30319 cp_parser_error (parser, "invalid type for property");
30320 declspecs.locations[ds_thread] = 0;
30321 }
30322
30323 /* typedef. */
30324 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30325 {
30326 cp_parser_error (parser, "invalid type for property");
30327 declspecs.locations[ds_typedef] = 0;
30328 }
30329
30330 prefix_attributes = declspecs.attributes;
30331 declspecs.attributes = NULL_TREE;
30332
30333 /* Keep going until we hit the `;' at the end of the declaration. */
30334 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30335 {
30336 tree attributes, first_attribute, decl;
30337 cp_declarator *declarator;
30338 cp_token *token;
30339
30340 /* Parse the declarator. */
30341 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30342 NULL, NULL, false, false);
30343
30344 /* Look for attributes that apply to the ivar. */
30345 attributes = cp_parser_attributes_opt (parser);
30346 /* Remember which attributes are prefix attributes and
30347 which are not. */
30348 first_attribute = attributes;
30349 /* Combine the attributes. */
30350 attributes = attr_chainon (prefix_attributes, attributes);
30351
30352 decl = grokfield (declarator, &declspecs,
30353 NULL_TREE, /*init_const_expr_p=*/false,
30354 NULL_TREE, attributes);
30355
30356 if (decl == error_mark_node || decl == NULL_TREE)
30357 return error_mark_node;
30358
30359 /* Reset PREFIX_ATTRIBUTES. */
30360 if (attributes != error_mark_node)
30361 {
30362 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30363 attributes = TREE_CHAIN (attributes);
30364 if (attributes)
30365 TREE_CHAIN (attributes) = NULL_TREE;
30366 }
30367
30368 DECL_CHAIN (decl) = decls;
30369 decls = decl;
30370
30371 token = cp_lexer_peek_token (parser->lexer);
30372 if (token->type == CPP_COMMA)
30373 {
30374 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30375 continue;
30376 }
30377 else
30378 break;
30379 }
30380 return decls;
30381}
30382
30383/* Parse an Objective-C @property declaration. The syntax is:
30384
30385 objc-property-declaration:
30386 '@property' objc-property-attributes[opt] struct-declaration ;
30387
30388 objc-property-attributes:
30389 '(' objc-property-attribute-list ')'
30390
30391 objc-property-attribute-list:
30392 objc-property-attribute
30393 objc-property-attribute-list, objc-property-attribute
30394
30395 objc-property-attribute
30396 'getter' = identifier
30397 'setter' = identifier
30398 'readonly'
30399 'readwrite'
30400 'assign'
30401 'retain'
30402 'copy'
30403 'nonatomic'
30404
30405 For example:
30406 @property NSString *name;
30407 @property (readonly) id object;
30408 @property (retain, nonatomic, getter=getTheName) id name;
30409 @property int a, b, c;
30410
30411 PS: This function is identical to
30412 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30413static void
30414cp_parser_objc_at_property_declaration (cp_parser *parser)
30415{
30416 /* The following variables hold the attributes of the properties as
30417 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30418 seen. When we see an attribute, we set them to 'true' (if they
30419 are boolean properties) or to the identifier (if they have an
30420 argument, ie, for getter and setter). Note that here we only
30421 parse the list of attributes, check the syntax and accumulate the
30422 attributes that we find. objc_add_property_declaration() will
30423 then process the information. */
30424 bool property_assign = false;
30425 bool property_copy = false;
30426 tree property_getter_ident = NULL_TREE;
30427 bool property_nonatomic = false;
30428 bool property_readonly = false;
30429 bool property_readwrite = false;
30430 bool property_retain = false;
30431 tree property_setter_ident = NULL_TREE;
30432
30433 /* 'properties' is the list of properties that we read. Usually a
30434 single one, but maybe more (eg, in "@property int a, b, c;" there
30435 are three). */
30436 tree properties;
30437 location_t loc;
30438
30439 loc = cp_lexer_peek_token (parser->lexer)->location;
30440
30441 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30442
30443 /* Parse the optional attribute list... */
30444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30445 {
30446 /* Eat the '('. */
30447 cp_lexer_consume_token (parser->lexer);
30448
30449 while (true)
30450 {
30451 bool syntax_error = false;
30452 cp_token *token = cp_lexer_peek_token (parser->lexer);
30453 enum rid keyword;
30454
30455 if (token->type != CPP_NAME)
30456 {
30457 cp_parser_error (parser, "expected identifier");
30458 break;
30459 }
30460 keyword = C_RID_CODE (token->u.value);
30461 cp_lexer_consume_token (parser->lexer);
30462 switch (keyword)
30463 {
30464 case RID_ASSIGN: property_assign = true; break;
30465 case RID_COPY: property_copy = true; break;
30466 case RID_NONATOMIC: property_nonatomic = true; break;
30467 case RID_READONLY: property_readonly = true; break;
30468 case RID_READWRITE: property_readwrite = true; break;
30469 case RID_RETAIN: property_retain = true; break;
30470
30471 case RID_GETTER:
30472 case RID_SETTER:
30473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30474 {
30475 if (keyword == RID_GETTER)
30476 cp_parser_error (parser,
30477 "missing %<=%> (after %<getter%> attribute)");
30478 else
30479 cp_parser_error (parser,
30480 "missing %<=%> (after %<setter%> attribute)");
30481 syntax_error = true;
30482 break;
30483 }
30484 cp_lexer_consume_token (parser->lexer); /* eat the = */
30485 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30486 {
30487 cp_parser_error (parser, "expected identifier");
30488 syntax_error = true;
30489 break;
30490 }
30491 if (keyword == RID_SETTER)
30492 {
30493 if (property_setter_ident != NULL_TREE)
30494 {
30495 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30496 cp_lexer_consume_token (parser->lexer);
30497 }
30498 else
30499 property_setter_ident = cp_parser_objc_selector (parser);
30500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30501 cp_parser_error (parser, "setter name must terminate with %<:%>");
30502 else
30503 cp_lexer_consume_token (parser->lexer);
30504 }
30505 else
30506 {
30507 if (property_getter_ident != NULL_TREE)
30508 {
30509 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30510 cp_lexer_consume_token (parser->lexer);
30511 }
30512 else
30513 property_getter_ident = cp_parser_objc_selector (parser);
30514 }
30515 break;
30516 default:
30517 cp_parser_error (parser, "unknown property attribute");
30518 syntax_error = true;
30519 break;
30520 }
30521
30522 if (syntax_error)
30523 break;
30524
30525 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30526 cp_lexer_consume_token (parser->lexer);
30527 else
30528 break;
30529 }
30530
30531 /* FIXME: "@property (setter, assign);" will generate a spurious
30532 "error: expected ‘)’ before ‘,’ token". This is because
30533 cp_parser_require, unlike the C counterpart, will produce an
30534 error even if we are in error recovery. */
30535 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30536 {
30537 cp_parser_skip_to_closing_parenthesis (parser,
30538 /*recovering=*/true,
30539 /*or_comma=*/false,
30540 /*consume_paren=*/true);
30541 }
30542 }
30543
30544 /* ... and the property declaration(s). */
30545 properties = cp_parser_objc_struct_declaration (parser);
30546
30547 if (properties == error_mark_node)
30548 {
30549 cp_parser_skip_to_end_of_statement (parser);
30550 /* If the next token is now a `;', consume it. */
30551 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30552 cp_lexer_consume_token (parser->lexer);
30553 return;
30554 }
30555
30556 if (properties == NULL_TREE)
30557 cp_parser_error (parser, "expected identifier");
30558 else
30559 {
30560 /* Comma-separated properties are chained together in
30561 reverse order; add them one by one. */
30562 properties = nreverse (properties);
30563
30564 for (; properties; properties = TREE_CHAIN (properties))
30565 objc_add_property_declaration (loc, copy_node (properties),
30566 property_readonly, property_readwrite,
30567 property_assign, property_retain,
30568 property_copy, property_nonatomic,
30569 property_getter_ident, property_setter_ident);
30570 }
30571
30572 cp_parser_consume_semicolon_at_end_of_statement (parser);
30573}
30574
30575/* Parse an Objective-C++ @synthesize declaration. The syntax is:
30576
30577 objc-synthesize-declaration:
30578 @synthesize objc-synthesize-identifier-list ;
30579
30580 objc-synthesize-identifier-list:
30581 objc-synthesize-identifier
30582 objc-synthesize-identifier-list, objc-synthesize-identifier
30583
30584 objc-synthesize-identifier
30585 identifier
30586 identifier = identifier
30587
30588 For example:
30589 @synthesize MyProperty;
30590 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30591
30592 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30593 for C. Keep them in sync.
30594*/
30595static void
30596cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30597{
30598 tree list = NULL_TREE;
30599 location_t loc;
30600 loc = cp_lexer_peek_token (parser->lexer)->location;
30601
30602 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30603 while (true)
30604 {
30605 tree property, ivar;
30606 property = cp_parser_identifier (parser);
30607 if (property == error_mark_node)
30608 {
30609 cp_parser_consume_semicolon_at_end_of_statement (parser);
30610 return;
30611 }
30612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30613 {
30614 cp_lexer_consume_token (parser->lexer);
30615 ivar = cp_parser_identifier (parser);
30616 if (ivar == error_mark_node)
30617 {
30618 cp_parser_consume_semicolon_at_end_of_statement (parser);
30619 return;
30620 }
30621 }
30622 else
30623 ivar = NULL_TREE;
30624 list = chainon (list, build_tree_list (ivar, property));
30625 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30626 cp_lexer_consume_token (parser->lexer);
30627 else
30628 break;
30629 }
30630 cp_parser_consume_semicolon_at_end_of_statement (parser);
30631 objc_add_synthesize_declaration (loc, list);
30632}
30633
30634/* Parse an Objective-C++ @dynamic declaration. The syntax is:
30635
30636 objc-dynamic-declaration:
30637 @dynamic identifier-list ;
30638
30639 For example:
30640 @dynamic MyProperty;
30641 @dynamic MyProperty, AnotherProperty;
30642
30643 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30644 for C. Keep them in sync.
30645*/
30646static void
30647cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30648{
30649 tree list = NULL_TREE;
30650 location_t loc;
30651 loc = cp_lexer_peek_token (parser->lexer)->location;
30652
30653 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30654 while (true)
30655 {
30656 tree property;
30657 property = cp_parser_identifier (parser);
30658 if (property == error_mark_node)
30659 {
30660 cp_parser_consume_semicolon_at_end_of_statement (parser);
30661 return;
30662 }
30663 list = chainon (list, build_tree_list (NULL, property));
30664 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30665 cp_lexer_consume_token (parser->lexer);
30666 else
30667 break;
30668 }
30669 cp_parser_consume_semicolon_at_end_of_statement (parser);
30670 objc_add_dynamic_declaration (loc, list);
30671}
30672
30673
30674/* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30675
30676/* Returns name of the next clause.
30677 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30678 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30679 returned and the token is consumed. */
30680
30681static pragma_omp_clause
30682cp_parser_omp_clause_name (cp_parser *parser)
30683{
30684 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30685
30686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30687 result = PRAGMA_OACC_CLAUSE_AUTO;
30688 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30689 result = PRAGMA_OMP_CLAUSE_IF;
30690 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30691 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30692 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30693 result = PRAGMA_OACC_CLAUSE_DELETE;
30694 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30695 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30696 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30697 result = PRAGMA_OMP_CLAUSE_FOR;
30698 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30699 {
30700 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30701 const char *p = IDENTIFIER_POINTER (id);
30702
30703 switch (p[0])
30704 {
30705 case 'a':
30706 if (!strcmp ("aligned", p))
30707 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30708 else if (!strcmp ("async", p))
30709 result = PRAGMA_OACC_CLAUSE_ASYNC;
30710 break;
30711 case 'c':
30712 if (!strcmp ("collapse", p))
30713 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30714 else if (!strcmp ("copy", p))
30715 result = PRAGMA_OACC_CLAUSE_COPY;
30716 else if (!strcmp ("copyin", p))
30717 result = PRAGMA_OMP_CLAUSE_COPYIN;
30718 else if (!strcmp ("copyout", p))
30719 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30720 else if (!strcmp ("copyprivate", p))
30721 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30722 else if (!strcmp ("create", p))
30723 result = PRAGMA_OACC_CLAUSE_CREATE;
30724 break;
30725 case 'd':
30726 if (!strcmp ("defaultmap", p))
30727 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30728 else if (!strcmp ("depend", p))
30729 result = PRAGMA_OMP_CLAUSE_DEPEND;
30730 else if (!strcmp ("device", p))
30731 result = PRAGMA_OMP_CLAUSE_DEVICE;
30732 else if (!strcmp ("deviceptr", p))
30733 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30734 else if (!strcmp ("device_resident", p))
30735 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30736 else if (!strcmp ("dist_schedule", p))
30737 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30738 break;
30739 case 'f':
30740 if (!strcmp ("final", p))
30741 result = PRAGMA_OMP_CLAUSE_FINAL;
30742 else if (!strcmp ("firstprivate", p))
30743 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30744 else if (!strcmp ("from", p))
30745 result = PRAGMA_OMP_CLAUSE_FROM;
30746 break;
30747 case 'g':
30748 if (!strcmp ("gang", p))
30749 result = PRAGMA_OACC_CLAUSE_GANG;
30750 else if (!strcmp ("grainsize", p))
30751 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30752 break;
30753 case 'h':
30754 if (!strcmp ("hint", p))
30755 result = PRAGMA_OMP_CLAUSE_HINT;
30756 else if (!strcmp ("host", p))
30757 result = PRAGMA_OACC_CLAUSE_HOST;
30758 break;
30759 case 'i':
30760 if (!strcmp ("inbranch", p))
30761 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30762 else if (!strcmp ("independent", p))
30763 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30764 else if (!strcmp ("is_device_ptr", p))
30765 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30766 break;
30767 case 'l':
30768 if (!strcmp ("lastprivate", p))
30769 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30770 else if (!strcmp ("linear", p))
30771 result = PRAGMA_OMP_CLAUSE_LINEAR;
30772 else if (!strcmp ("link", p))
30773 result = PRAGMA_OMP_CLAUSE_LINK;
30774 break;
30775 case 'm':
30776 if (!strcmp ("map", p))
30777 result = PRAGMA_OMP_CLAUSE_MAP;
30778 else if (!strcmp ("mergeable", p))
30779 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30780 else if (flag_cilkplus && !strcmp ("mask", p))
30781 result = PRAGMA_CILK_CLAUSE_MASK;
30782 break;
30783 case 'n':
30784 if (!strcmp ("nogroup", p))
30785 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30786 else if (!strcmp ("notinbranch", p))
30787 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30788 else if (!strcmp ("nowait", p))
30789 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30790 else if (flag_cilkplus && !strcmp ("nomask", p))
30791 result = PRAGMA_CILK_CLAUSE_NOMASK;
30792 else if (!strcmp ("num_gangs", p))
30793 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30794 else if (!strcmp ("num_tasks", p))
30795 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30796 else if (!strcmp ("num_teams", p))
30797 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30798 else if (!strcmp ("num_threads", p))
30799 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30800 else if (!strcmp ("num_workers", p))
30801 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30802 break;
30803 case 'o':
30804 if (!strcmp ("ordered", p))
30805 result = PRAGMA_OMP_CLAUSE_ORDERED;
30806 break;
30807 case 'p':
30808 if (!strcmp ("parallel", p))
30809 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30810 else if (!strcmp ("present", p))
30811 result = PRAGMA_OACC_CLAUSE_PRESENT;
30812 else if (!strcmp ("present_or_copy", p)
30813 || !strcmp ("pcopy", p))
30814 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30815 else if (!strcmp ("present_or_copyin", p)
30816 || !strcmp ("pcopyin", p))
30817 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30818 else if (!strcmp ("present_or_copyout", p)
30819 || !strcmp ("pcopyout", p))
30820 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30821 else if (!strcmp ("present_or_create", p)
30822 || !strcmp ("pcreate", p))
30823 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30824 else if (!strcmp ("priority", p))
30825 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30826 else if (!strcmp ("proc_bind", p))
30827 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30828 break;
30829 case 'r':
30830 if (!strcmp ("reduction", p))
30831 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30832 break;
30833 case 's':
30834 if (!strcmp ("safelen", p))
30835 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30836 else if (!strcmp ("schedule", p))
30837 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30838 else if (!strcmp ("sections", p))
30839 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30840 else if (!strcmp ("self", p))
30841 result = PRAGMA_OACC_CLAUSE_SELF;
30842 else if (!strcmp ("seq", p))
30843 result = PRAGMA_OACC_CLAUSE_SEQ;
30844 else if (!strcmp ("shared", p))
30845 result = PRAGMA_OMP_CLAUSE_SHARED;
30846 else if (!strcmp ("simd", p))
30847 result = PRAGMA_OMP_CLAUSE_SIMD;
30848 else if (!strcmp ("simdlen", p))
30849 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30850 break;
30851 case 't':
30852 if (!strcmp ("taskgroup", p))
30853 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30854 else if (!strcmp ("thread_limit", p))
30855 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30856 else if (!strcmp ("threads", p))
30857 result = PRAGMA_OMP_CLAUSE_THREADS;
30858 else if (!strcmp ("tile", p))
30859 result = PRAGMA_OACC_CLAUSE_TILE;
30860 else if (!strcmp ("to", p))
30861 result = PRAGMA_OMP_CLAUSE_TO;
30862 break;
30863 case 'u':
30864 if (!strcmp ("uniform", p))
30865 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30866 else if (!strcmp ("untied", p))
30867 result = PRAGMA_OMP_CLAUSE_UNTIED;
30868 else if (!strcmp ("use_device", p))
30869 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30870 else if (!strcmp ("use_device_ptr", p))
30871 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30872 break;
30873 case 'v':
30874 if (!strcmp ("vector", p))
30875 result = PRAGMA_OACC_CLAUSE_VECTOR;
30876 else if (!strcmp ("vector_length", p))
30877 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30878 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30879 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30880 break;
30881 case 'w':
30882 if (!strcmp ("wait", p))
30883 result = PRAGMA_OACC_CLAUSE_WAIT;
30884 else if (!strcmp ("worker", p))
30885 result = PRAGMA_OACC_CLAUSE_WORKER;
30886 break;
30887 }
30888 }
30889
30890 if (result != PRAGMA_OMP_CLAUSE_NONE)
30891 cp_lexer_consume_token (parser->lexer);
30892
30893 return result;
30894}
30895
30896/* Validate that a clause of the given type does not already exist. */
30897
30898static void
30899check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30900 const char *name, location_t location)
30901{
30902 tree c;
30903
30904 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30905 if (OMP_CLAUSE_CODE (c) == code)
30906 {
30907 error_at (location, "too many %qs clauses", name);
30908 break;
30909 }
30910}
30911
30912/* OpenMP 2.5:
30913 variable-list:
30914 identifier
30915 variable-list , identifier
30916
30917 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30918 colon). An opening parenthesis will have been consumed by the caller.
30919
30920 If KIND is nonzero, create the appropriate node and install the decl
30921 in OMP_CLAUSE_DECL and add the node to the head of the list.
30922
30923 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30924 return the list created.
30925
30926 COLON can be NULL if only closing parenthesis should end the list,
30927 or pointer to bool which will receive false if the list is terminated
30928 by closing parenthesis or true if the list is terminated by colon. */
30929
30930static tree
30931cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30932 tree list, bool *colon)
30933{
30934 cp_token *token;
30935 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30936 if (colon)
30937 {
30938 parser->colon_corrects_to_scope_p = false;
30939 *colon = false;
30940 }
30941 while (1)
30942 {
30943 tree name, decl;
30944
30945 token = cp_lexer_peek_token (parser->lexer);
30946 if (kind != 0
30947 && current_class_ptr
30948 && cp_parser_is_keyword (token, RID_THIS))
30949 {
30950 decl = finish_this_expr ();
30951 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30952 || CONVERT_EXPR_P (decl))
30953 decl = TREE_OPERAND (decl, 0);
30954 cp_lexer_consume_token (parser->lexer);
30955 }
30956 else
30957 {
30958 name = cp_parser_id_expression (parser, /*template_p=*/false,
30959 /*check_dependency_p=*/true,
30960 /*template_p=*/NULL,
30961 /*declarator_p=*/false,
30962 /*optional_p=*/false);
30963 if (name == error_mark_node)
30964 goto skip_comma;
30965
30966 if (identifier_p (name))
30967 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30968 else
30969 decl = name;
30970 if (decl == error_mark_node)
30971 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30972 token->location);
30973 }
30974 if (decl == error_mark_node)
30975 ;
30976 else if (kind != 0)
30977 {
30978 switch (kind)
30979 {
30980 case OMP_CLAUSE__CACHE_:
30981 /* The OpenACC cache directive explicitly only allows "array
30982 elements or subarrays". */
30983 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30984 {
30985 error_at (token->location, "expected %<[%>");
30986 decl = error_mark_node;
30987 break;
30988 }
30989 /* FALLTHROUGH. */
30990 case OMP_CLAUSE_MAP:
30991 case OMP_CLAUSE_FROM:
30992 case OMP_CLAUSE_TO:
30993 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30994 {
30995 location_t loc
30996 = cp_lexer_peek_token (parser->lexer)->location;
30997 cp_id_kind idk = CP_ID_KIND_NONE;
30998 cp_lexer_consume_token (parser->lexer);
30999 decl = convert_from_reference (decl);
31000 decl
31001 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31002 decl, false,
31003 &idk, loc);
31004 }
31005 /* FALLTHROUGH. */
31006 case OMP_CLAUSE_DEPEND:
31007 case OMP_CLAUSE_REDUCTION:
31008 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31009 {
31010 tree low_bound = NULL_TREE, length = NULL_TREE;
31011
31012 parser->colon_corrects_to_scope_p = false;
31013 cp_lexer_consume_token (parser->lexer);
31014 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31015 low_bound = cp_parser_expression (parser);
31016 if (!colon)
31017 parser->colon_corrects_to_scope_p
31018 = saved_colon_corrects_to_scope_p;
31019 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31020 length = integer_one_node;
31021 else
31022 {
31023 /* Look for `:'. */
31024 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31025 goto skip_comma;
31026 if (!cp_lexer_next_token_is (parser->lexer,
31027 CPP_CLOSE_SQUARE))
31028 length = cp_parser_expression (parser);
31029 }
31030 /* Look for the closing `]'. */
31031 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31032 RT_CLOSE_SQUARE))
31033 goto skip_comma;
31034
31035 decl = tree_cons (low_bound, length, decl);
31036 }
31037 break;
31038 default:
31039 break;
31040 }
31041
31042 tree u = build_omp_clause (token->location, kind);
31043 OMP_CLAUSE_DECL (u) = decl;
31044 OMP_CLAUSE_CHAIN (u) = list;
31045 list = u;
31046 }
31047 else
31048 list = tree_cons (decl, NULL_TREE, list);
31049
31050 get_comma:
31051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31052 break;
31053 cp_lexer_consume_token (parser->lexer);
31054 }
31055
31056 if (colon)
31057 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31058
31059 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31060 {
31061 *colon = true;
31062 cp_parser_require (parser, CPP_COLON, RT_COLON);
31063 return list;
31064 }
31065
31066 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31067 {
31068 int ending;
31069
31070 /* Try to resync to an unnested comma. Copied from
31071 cp_parser_parenthesized_expression_list. */
31072 skip_comma:
31073 if (colon)
31074 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31075 ending = cp_parser_skip_to_closing_parenthesis (parser,
31076 /*recovering=*/true,
31077 /*or_comma=*/true,
31078 /*consume_paren=*/true);
31079 if (ending < 0)
31080 goto get_comma;
31081 }
31082
31083 return list;
31084}
31085
31086/* Similarly, but expect leading and trailing parenthesis. This is a very
31087 common case for omp clauses. */
31088
31089static tree
31090cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31091{
31092 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31093 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31094 return list;
31095}
31096
31097/* OpenACC 2.0:
31098 copy ( variable-list )
31099 copyin ( variable-list )
31100 copyout ( variable-list )
31101 create ( variable-list )
31102 delete ( variable-list )
31103 present ( variable-list )
31104 present_or_copy ( variable-list )
31105 pcopy ( variable-list )
31106 present_or_copyin ( variable-list )
31107 pcopyin ( variable-list )
31108 present_or_copyout ( variable-list )
31109 pcopyout ( variable-list )
31110 present_or_create ( variable-list )
31111 pcreate ( variable-list ) */
31112
31113static tree
31114cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31115 tree list)
31116{
31117 enum gomp_map_kind kind;
31118 switch (c_kind)
31119 {
31120 case PRAGMA_OACC_CLAUSE_COPY:
31121 kind = GOMP_MAP_FORCE_TOFROM;
31122 break;
31123 case PRAGMA_OACC_CLAUSE_COPYIN:
31124 kind = GOMP_MAP_FORCE_TO;
31125 break;
31126 case PRAGMA_OACC_CLAUSE_COPYOUT:
31127 kind = GOMP_MAP_FORCE_FROM;
31128 break;
31129 case PRAGMA_OACC_CLAUSE_CREATE:
31130 kind = GOMP_MAP_FORCE_ALLOC;
31131 break;
31132 case PRAGMA_OACC_CLAUSE_DELETE:
31133 kind = GOMP_MAP_DELETE;
31134 break;
31135 case PRAGMA_OACC_CLAUSE_DEVICE:
31136 kind = GOMP_MAP_FORCE_TO;
31137 break;
31138 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31139 kind = GOMP_MAP_DEVICE_RESIDENT;
31140 break;
31141 case PRAGMA_OACC_CLAUSE_HOST:
31142 case PRAGMA_OACC_CLAUSE_SELF:
31143 kind = GOMP_MAP_FORCE_FROM;
31144 break;
31145 case PRAGMA_OACC_CLAUSE_LINK:
31146 kind = GOMP_MAP_LINK;
31147 break;
31148 case PRAGMA_OACC_CLAUSE_PRESENT:
31149 kind = GOMP_MAP_FORCE_PRESENT;
31150 break;
31151 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31152 kind = GOMP_MAP_TOFROM;
31153 break;
31154 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31155 kind = GOMP_MAP_TO;
31156 break;
31157 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31158 kind = GOMP_MAP_FROM;
31159 break;
31160 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31161 kind = GOMP_MAP_ALLOC;
31162 break;
31163 default:
31164 gcc_unreachable ();
31165 }
31166 tree nl, c;
31167 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31168
31169 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31170 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31171
31172 return nl;
31173}
31174
31175/* OpenACC 2.0:
31176 deviceptr ( variable-list ) */
31177
31178static tree
31179cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31180{
31181 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31182 tree vars, t;
31183
31184 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31185 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31186 variable-list must only allow for pointer variables. */
31187 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31188 for (t = vars; t; t = TREE_CHAIN (t))
31189 {
31190 tree v = TREE_PURPOSE (t);
31191 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31192 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31193 OMP_CLAUSE_DECL (u) = v;
31194 OMP_CLAUSE_CHAIN (u) = list;
31195 list = u;
31196 }
31197
31198 return list;
31199}
31200
31201/* OpenACC 2.0:
31202 auto
31203 independent
31204 nohost
31205 seq */
31206
31207static tree
31208cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31209 enum omp_clause_code code,
31210 tree list, location_t location)
31211{
31212 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31213 tree c = build_omp_clause (location, code);
31214 OMP_CLAUSE_CHAIN (c) = list;
31215 return c;
31216}
31217
31218 /* OpenACC:
31219 num_gangs ( expression )
31220 num_workers ( expression )
31221 vector_length ( expression ) */
31222
31223static tree
31224cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31225 const char *str, tree list)
31226{
31227 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31228
31229 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31230 return list;
31231
31232 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31233
31234 if (t == error_mark_node
31235 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31236 {
31237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31238 /*or_comma=*/false,
31239 /*consume_paren=*/true);
31240 return list;
31241 }
31242
31243 check_no_duplicate_clause (list, code, str, loc);
31244
31245 tree c = build_omp_clause (loc, code);
31246 OMP_CLAUSE_OPERAND (c, 0) = t;
31247 OMP_CLAUSE_CHAIN (c) = list;
31248 return c;
31249}
31250
31251/* OpenACC:
31252
31253 gang [( gang-arg-list )]
31254 worker [( [num:] int-expr )]
31255 vector [( [length:] int-expr )]
31256
31257 where gang-arg is one of:
31258
31259 [num:] int-expr
31260 static: size-expr
31261
31262 and size-expr may be:
31263
31264 *
31265 int-expr
31266*/
31267
31268static tree
31269cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31270 const char *str, tree list)
31271{
31272 const char *id = "num";
31273 cp_lexer *lexer = parser->lexer;
31274 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31275 location_t loc = cp_lexer_peek_token (lexer)->location;
31276
31277 if (kind == OMP_CLAUSE_VECTOR)
31278 id = "length";
31279
31280 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31281 {
31282 cp_lexer_consume_token (lexer);
31283
31284 do
31285 {
31286 cp_token *next = cp_lexer_peek_token (lexer);
31287 int idx = 0;
31288
31289 /* Gang static argument. */
31290 if (kind == OMP_CLAUSE_GANG
31291 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31292 {
31293 cp_lexer_consume_token (lexer);
31294
31295 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31296 goto cleanup_error;
31297
31298 idx = 1;
31299 if (ops[idx] != NULL)
31300 {
31301 cp_parser_error (parser, "too many %<static%> arguments");
31302 goto cleanup_error;
31303 }
31304
31305 /* Check for the '*' argument. */
31306 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31307 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31308 || cp_lexer_nth_token_is (parser->lexer, 2,
31309 CPP_CLOSE_PAREN)))
31310 {
31311 cp_lexer_consume_token (lexer);
31312 ops[idx] = integer_minus_one_node;
31313
31314 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31315 {
31316 cp_lexer_consume_token (lexer);
31317 continue;
31318 }
31319 else break;
31320 }
31321 }
31322 /* Worker num: argument and vector length: arguments. */
31323 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31324 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31325 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31326 {
31327 cp_lexer_consume_token (lexer); /* id */
31328 cp_lexer_consume_token (lexer); /* ':' */
31329 }
31330
31331 /* Now collect the actual argument. */
31332 if (ops[idx] != NULL_TREE)
31333 {
31334 cp_parser_error (parser, "unexpected argument");
31335 goto cleanup_error;
31336 }
31337
31338 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31339 false);
31340 if (expr == error_mark_node)
31341 goto cleanup_error;
31342
31343 mark_exp_read (expr);
31344 ops[idx] = expr;
31345
31346 if (kind == OMP_CLAUSE_GANG
31347 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31348 {
31349 cp_lexer_consume_token (lexer);
31350 continue;
31351 }
31352 break;
31353 }
31354 while (1);
31355
31356 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31357 goto cleanup_error;
31358 }
31359
31360 check_no_duplicate_clause (list, kind, str, loc);
31361
31362 c = build_omp_clause (loc, kind);
31363
31364 if (ops[1])
31365 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31366
31367 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31368 OMP_CLAUSE_CHAIN (c) = list;
31369
31370 return c;
31371
31372 cleanup_error:
31373 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31374 return list;
31375}
31376
31377/* OpenACC 2.0:
31378 tile ( size-expr-list ) */
31379
31380static tree
31381cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31382{
31383 tree c, expr = error_mark_node;
31384 tree tile = NULL_TREE;
31385
31386 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31387 so, but the spec authors never considered such a case and have
31388 differing opinions on what it might mean, including 'not
31389 allowed'.) */
31390 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31391 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31392 clause_loc);
31393
31394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31395 return list;
31396
31397 do
31398 {
31399 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31400 return list;
31401
31402 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31403 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31404 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31405 {
31406 cp_lexer_consume_token (parser->lexer);
31407 expr = integer_zero_node;
31408 }
31409 else
31410 expr = cp_parser_constant_expression (parser);
31411
31412 tile = tree_cons (NULL_TREE, expr, tile);
31413 }
31414 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31415
31416 /* Consume the trailing ')'. */
31417 cp_lexer_consume_token (parser->lexer);
31418
31419 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31420 tile = nreverse (tile);
31421 OMP_CLAUSE_TILE_LIST (c) = tile;
31422 OMP_CLAUSE_CHAIN (c) = list;
31423 return c;
31424}
31425
31426/* OpenACC 2.0
31427 Parse wait clause or directive parameters. */
31428
31429static tree
31430cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31431{
31432 vec<tree, va_gc> *args;
31433 tree t, args_tree;
31434
31435 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31436 /*cast_p=*/false,
31437 /*allow_expansion_p=*/true,
31438 /*non_constant_p=*/NULL);
31439
31440 if (args == NULL || args->length () == 0)
31441 {
31442 cp_parser_error (parser, "expected integer expression before ')'");
31443 if (args != NULL)
31444 release_tree_vector (args);
31445 return list;
31446 }
31447
31448 args_tree = build_tree_list_vec (args);
31449
31450 release_tree_vector (args);
31451
31452 for (t = args_tree; t; t = TREE_CHAIN (t))
31453 {
31454 tree targ = TREE_VALUE (t);
31455
31456 if (targ != error_mark_node)
31457 {
31458 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31459 error ("%<wait%> expression must be integral");
31460 else
31461 {
31462 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31463
31464 mark_rvalue_use (targ);
31465 OMP_CLAUSE_DECL (c) = targ;
31466 OMP_CLAUSE_CHAIN (c) = list;
31467 list = c;
31468 }
31469 }
31470 }
31471
31472 return list;
31473}
31474
31475/* OpenACC:
31476 wait ( int-expr-list ) */
31477
31478static tree
31479cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31480{
31481 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31482
31483 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31484 return list;
31485
31486 list = cp_parser_oacc_wait_list (parser, location, list);
31487
31488 return list;
31489}
31490
31491/* OpenMP 3.0:
31492 collapse ( constant-expression ) */
31493
31494static tree
31495cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31496{
31497 tree c, num;
31498 location_t loc;
31499 HOST_WIDE_INT n;
31500
31501 loc = cp_lexer_peek_token (parser->lexer)->location;
31502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31503 return list;
31504
31505 num = cp_parser_constant_expression (parser);
31506
31507 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31509 /*or_comma=*/false,
31510 /*consume_paren=*/true);
31511
31512 if (num == error_mark_node)
31513 return list;
31514 num = fold_non_dependent_expr (num);
31515 if (!tree_fits_shwi_p (num)
31516 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31517 || (n = tree_to_shwi (num)) <= 0
31518 || (int) n != n)
31519 {
31520 error_at (loc, "collapse argument needs positive constant integer expression");
31521 return list;
31522 }
31523
31524 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31525 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31526 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31527 OMP_CLAUSE_CHAIN (c) = list;
31528 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31529
31530 return c;
31531}
31532
31533/* OpenMP 2.5:
31534 default ( shared | none )
31535
31536 OpenACC 2.0
31537 default (none) */
31538
31539static tree
31540cp_parser_omp_clause_default (cp_parser *parser, tree list,
31541 location_t location, bool is_oacc)
31542{
31543 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31544 tree c;
31545
31546 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31547 return list;
31548 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31549 {
31550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31551 const char *p = IDENTIFIER_POINTER (id);
31552
31553 switch (p[0])
31554 {
31555 case 'n':
31556 if (strcmp ("none", p) != 0)
31557 goto invalid_kind;
31558 kind = OMP_CLAUSE_DEFAULT_NONE;
31559 break;
31560
31561 case 's':
31562 if (strcmp ("shared", p) != 0 || is_oacc)
31563 goto invalid_kind;
31564 kind = OMP_CLAUSE_DEFAULT_SHARED;
31565 break;
31566
31567 default:
31568 goto invalid_kind;
31569 }
31570
31571 cp_lexer_consume_token (parser->lexer);
31572 }
31573 else
31574 {
31575 invalid_kind:
31576 if (is_oacc)
31577 cp_parser_error (parser, "expected %<none%>");
31578 else
31579 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31580 }
31581
31582 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31583 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31584 /*or_comma=*/false,
31585 /*consume_paren=*/true);
31586
31587 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31588 return list;
31589
31590 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31591 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31592 OMP_CLAUSE_CHAIN (c) = list;
31593 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31594
31595 return c;
31596}
31597
31598/* OpenMP 3.1:
31599 final ( expression ) */
31600
31601static tree
31602cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31603{
31604 tree t, c;
31605
31606 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31607 return list;
31608
31609 t = cp_parser_condition (parser);
31610
31611 if (t == error_mark_node
31612 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31614 /*or_comma=*/false,
31615 /*consume_paren=*/true);
31616
31617 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31618
31619 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31620 OMP_CLAUSE_FINAL_EXPR (c) = t;
31621 OMP_CLAUSE_CHAIN (c) = list;
31622
31623 return c;
31624}
31625
31626/* OpenMP 2.5:
31627 if ( expression )
31628
31629 OpenMP 4.5:
31630 if ( directive-name-modifier : expression )
31631
31632 directive-name-modifier:
31633 parallel | task | taskloop | target data | target | target update
31634 | target enter data | target exit data */
31635
31636static tree
31637cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31638 bool is_omp)
31639{
31640 tree t, c;
31641 enum tree_code if_modifier = ERROR_MARK;
31642
31643 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31644 return list;
31645
31646 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31647 {
31648 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31649 const char *p = IDENTIFIER_POINTER (id);
31650 int n = 2;
31651
31652 if (strcmp ("parallel", p) == 0)
31653 if_modifier = OMP_PARALLEL;
31654 else if (strcmp ("task", p) == 0)
31655 if_modifier = OMP_TASK;
31656 else if (strcmp ("taskloop", p) == 0)
31657 if_modifier = OMP_TASKLOOP;
31658 else if (strcmp ("target", p) == 0)
31659 {
31660 if_modifier = OMP_TARGET;
31661 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31662 {
31663 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31664 p = IDENTIFIER_POINTER (id);
31665 if (strcmp ("data", p) == 0)
31666 if_modifier = OMP_TARGET_DATA;
31667 else if (strcmp ("update", p) == 0)
31668 if_modifier = OMP_TARGET_UPDATE;
31669 else if (strcmp ("enter", p) == 0)
31670 if_modifier = OMP_TARGET_ENTER_DATA;
31671 else if (strcmp ("exit", p) == 0)
31672 if_modifier = OMP_TARGET_EXIT_DATA;
31673 if (if_modifier != OMP_TARGET)
31674 n = 3;
31675 else
31676 {
31677 location_t loc
31678 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31679 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31680 "or %<exit%>");
31681 if_modifier = ERROR_MARK;
31682 }
31683 if (if_modifier == OMP_TARGET_ENTER_DATA
31684 || if_modifier == OMP_TARGET_EXIT_DATA)
31685 {
31686 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31687 {
31688 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31689 p = IDENTIFIER_POINTER (id);
31690 if (strcmp ("data", p) == 0)
31691 n = 4;
31692 }
31693 if (n != 4)
31694 {
31695 location_t loc
31696 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31697 error_at (loc, "expected %<data%>");
31698 if_modifier = ERROR_MARK;
31699 }
31700 }
31701 }
31702 }
31703 if (if_modifier != ERROR_MARK)
31704 {
31705 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31706 {
31707 while (n-- > 0)
31708 cp_lexer_consume_token (parser->lexer);
31709 }
31710 else
31711 {
31712 if (n > 2)
31713 {
31714 location_t loc
31715 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31716 error_at (loc, "expected %<:%>");
31717 }
31718 if_modifier = ERROR_MARK;
31719 }
31720 }
31721 }
31722
31723 t = cp_parser_condition (parser);
31724
31725 if (t == error_mark_node
31726 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31728 /*or_comma=*/false,
31729 /*consume_paren=*/true);
31730
31731 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31733 {
31734 if (if_modifier != ERROR_MARK
31735 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31736 {
31737 const char *p = NULL;
31738 switch (if_modifier)
31739 {
31740 case OMP_PARALLEL: p = "parallel"; break;
31741 case OMP_TASK: p = "task"; break;
31742 case OMP_TASKLOOP: p = "taskloop"; break;
31743 case OMP_TARGET_DATA: p = "target data"; break;
31744 case OMP_TARGET: p = "target"; break;
31745 case OMP_TARGET_UPDATE: p = "target update"; break;
31746 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31747 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31748 default: gcc_unreachable ();
31749 }
31750 error_at (location, "too many %<if%> clauses with %qs modifier",
31751 p);
31752 return list;
31753 }
31754 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31755 {
31756 if (!is_omp)
31757 error_at (location, "too many %<if%> clauses");
31758 else
31759 error_at (location, "too many %<if%> clauses without modifier");
31760 return list;
31761 }
31762 else if (if_modifier == ERROR_MARK
31763 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31764 {
31765 error_at (location, "if any %<if%> clause has modifier, then all "
31766 "%<if%> clauses have to use modifier");
31767 return list;
31768 }
31769 }
31770
31771 c = build_omp_clause (location, OMP_CLAUSE_IF);
31772 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31773 OMP_CLAUSE_IF_EXPR (c) = t;
31774 OMP_CLAUSE_CHAIN (c) = list;
31775
31776 return c;
31777}
31778
31779/* OpenMP 3.1:
31780 mergeable */
31781
31782static tree
31783cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31784 tree list, location_t location)
31785{
31786 tree c;
31787
31788 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31789 location);
31790
31791 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31792 OMP_CLAUSE_CHAIN (c) = list;
31793 return c;
31794}
31795
31796/* OpenMP 2.5:
31797 nowait */
31798
31799static tree
31800cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31801 tree list, location_t location)
31802{
31803 tree c;
31804
31805 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31806
31807 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31808 OMP_CLAUSE_CHAIN (c) = list;
31809 return c;
31810}
31811
31812/* OpenMP 2.5:
31813 num_threads ( expression ) */
31814
31815static tree
31816cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31817 location_t location)
31818{
31819 tree t, c;
31820
31821 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31822 return list;
31823
31824 t = cp_parser_expression (parser);
31825
31826 if (t == error_mark_node
31827 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31828 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31829 /*or_comma=*/false,
31830 /*consume_paren=*/true);
31831
31832 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31833 "num_threads", location);
31834
31835 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31836 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31837 OMP_CLAUSE_CHAIN (c) = list;
31838
31839 return c;
31840}
31841
31842/* OpenMP 4.5:
31843 num_tasks ( expression ) */
31844
31845static tree
31846cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31847 location_t location)
31848{
31849 tree t, c;
31850
31851 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31852 return list;
31853
31854 t = cp_parser_expression (parser);
31855
31856 if (t == error_mark_node
31857 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31859 /*or_comma=*/false,
31860 /*consume_paren=*/true);
31861
31862 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31863 "num_tasks", location);
31864
31865 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31866 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31867 OMP_CLAUSE_CHAIN (c) = list;
31868
31869 return c;
31870}
31871
31872/* OpenMP 4.5:
31873 grainsize ( expression ) */
31874
31875static tree
31876cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31877 location_t location)
31878{
31879 tree t, c;
31880
31881 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31882 return list;
31883
31884 t = cp_parser_expression (parser);
31885
31886 if (t == error_mark_node
31887 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31888 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31889 /*or_comma=*/false,
31890 /*consume_paren=*/true);
31891
31892 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31893 "grainsize", location);
31894
31895 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31896 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31897 OMP_CLAUSE_CHAIN (c) = list;
31898
31899 return c;
31900}
31901
31902/* OpenMP 4.5:
31903 priority ( expression ) */
31904
31905static tree
31906cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31907 location_t location)
31908{
31909 tree t, c;
31910
31911 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31912 return list;
31913
31914 t = cp_parser_expression (parser);
31915
31916 if (t == error_mark_node
31917 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31919 /*or_comma=*/false,
31920 /*consume_paren=*/true);
31921
31922 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31923 "priority", location);
31924
31925 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31926 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31927 OMP_CLAUSE_CHAIN (c) = list;
31928
31929 return c;
31930}
31931
31932/* OpenMP 4.5:
31933 hint ( expression ) */
31934
31935static tree
31936cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31937 location_t location)
31938{
31939 tree t, c;
31940
31941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31942 return list;
31943
31944 t = cp_parser_expression (parser);
31945
31946 if (t == error_mark_node
31947 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31948 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31949 /*or_comma=*/false,
31950 /*consume_paren=*/true);
31951
31952 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31953
31954 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31955 OMP_CLAUSE_HINT_EXPR (c) = t;
31956 OMP_CLAUSE_CHAIN (c) = list;
31957
31958 return c;
31959}
31960
31961/* OpenMP 4.5:
31962 defaultmap ( tofrom : scalar ) */
31963
31964static tree
31965cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31966 location_t location)
31967{
31968 tree c, id;
31969 const char *p;
31970
31971 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31972 return list;
31973
31974 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31975 {
31976 cp_parser_error (parser, "expected %<tofrom%>");
31977 goto out_err;
31978 }
31979 id = cp_lexer_peek_token (parser->lexer)->u.value;
31980 p = IDENTIFIER_POINTER (id);
31981 if (strcmp (p, "tofrom") != 0)
31982 {
31983 cp_parser_error (parser, "expected %<tofrom%>");
31984 goto out_err;
31985 }
31986 cp_lexer_consume_token (parser->lexer);
31987 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31988 goto out_err;
31989
31990 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31991 {
31992 cp_parser_error (parser, "expected %<scalar%>");
31993 goto out_err;
31994 }
31995 id = cp_lexer_peek_token (parser->lexer)->u.value;
31996 p = IDENTIFIER_POINTER (id);
31997 if (strcmp (p, "scalar") != 0)
31998 {
31999 cp_parser_error (parser, "expected %<scalar%>");
32000 goto out_err;
32001 }
32002 cp_lexer_consume_token (parser->lexer);
32003 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32004 goto out_err;
32005
32006 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32007 location);
32008
32009 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32010 OMP_CLAUSE_CHAIN (c) = list;
32011 return c;
32012
32013 out_err:
32014 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32015 /*or_comma=*/false,
32016 /*consume_paren=*/true);
32017 return list;
32018}
32019
32020/* OpenMP 2.5:
32021 ordered
32022
32023 OpenMP 4.5:
32024 ordered ( constant-expression ) */
32025
32026static tree
32027cp_parser_omp_clause_ordered (cp_parser *parser,
32028 tree list, location_t location)
32029{
32030 tree c, num = NULL_TREE;
32031 HOST_WIDE_INT n;
32032
32033 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32034 "ordered", location);
32035
32036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32037 {
32038 cp_lexer_consume_token (parser->lexer);
32039
32040 num = cp_parser_constant_expression (parser);
32041
32042 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32043 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32044 /*or_comma=*/false,
32045 /*consume_paren=*/true);
32046
32047 if (num == error_mark_node)
32048 return list;
32049 num = fold_non_dependent_expr (num);
32050 if (!tree_fits_shwi_p (num)
32051 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32052 || (n = tree_to_shwi (num)) <= 0
32053 || (int) n != n)
32054 {
32055 error_at (location,
32056 "ordered argument needs positive constant integer "
32057 "expression");
32058 return list;
32059 }
32060 }
32061
32062 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32063 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32064 OMP_CLAUSE_CHAIN (c) = list;
32065 return c;
32066}
32067
32068/* OpenMP 2.5:
32069 reduction ( reduction-operator : variable-list )
32070
32071 reduction-operator:
32072 One of: + * - & ^ | && ||
32073
32074 OpenMP 3.1:
32075
32076 reduction-operator:
32077 One of: + * - & ^ | && || min max
32078
32079 OpenMP 4.0:
32080
32081 reduction-operator:
32082 One of: + * - & ^ | && ||
32083 id-expression */
32084
32085static tree
32086cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32087{
32088 enum tree_code code = ERROR_MARK;
32089 tree nlist, c, id = NULL_TREE;
32090
32091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32092 return list;
32093
32094 switch (cp_lexer_peek_token (parser->lexer)->type)
32095 {
32096 case CPP_PLUS: code = PLUS_EXPR; break;
32097 case CPP_MULT: code = MULT_EXPR; break;
32098 case CPP_MINUS: code = MINUS_EXPR; break;
32099 case CPP_AND: code = BIT_AND_EXPR; break;
32100 case CPP_XOR: code = BIT_XOR_EXPR; break;
32101 case CPP_OR: code = BIT_IOR_EXPR; break;
32102 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32103 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32104 default: break;
32105 }
32106
32107 if (code != ERROR_MARK)
32108 cp_lexer_consume_token (parser->lexer);
32109 else
32110 {
32111 bool saved_colon_corrects_to_scope_p;
32112 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32113 parser->colon_corrects_to_scope_p = false;
32114 id = cp_parser_id_expression (parser, /*template_p=*/false,
32115 /*check_dependency_p=*/true,
32116 /*template_p=*/NULL,
32117 /*declarator_p=*/false,
32118 /*optional_p=*/false);
32119 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32120 if (identifier_p (id))
32121 {
32122 const char *p = IDENTIFIER_POINTER (id);
32123
32124 if (strcmp (p, "min") == 0)
32125 code = MIN_EXPR;
32126 else if (strcmp (p, "max") == 0)
32127 code = MAX_EXPR;
32128 else if (id == cp_operator_id (PLUS_EXPR))
32129 code = PLUS_EXPR;
32130 else if (id == cp_operator_id (MULT_EXPR))
32131 code = MULT_EXPR;
32132 else if (id == cp_operator_id (MINUS_EXPR))
32133 code = MINUS_EXPR;
32134 else if (id == cp_operator_id (BIT_AND_EXPR))
32135 code = BIT_AND_EXPR;
32136 else if (id == cp_operator_id (BIT_IOR_EXPR))
32137 code = BIT_IOR_EXPR;
32138 else if (id == cp_operator_id (BIT_XOR_EXPR))
32139 code = BIT_XOR_EXPR;
32140 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32141 code = TRUTH_ANDIF_EXPR;
32142 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32143 code = TRUTH_ORIF_EXPR;
32144 id = omp_reduction_id (code, id, NULL_TREE);
32145 tree scope = parser->scope;
32146 if (scope)
32147 id = build_qualified_name (NULL_TREE, scope, id, false);
32148 parser->scope = NULL_TREE;
32149 parser->qualifying_scope = NULL_TREE;
32150 parser->object_scope = NULL_TREE;
32151 }
32152 else
32153 {
32154 error ("invalid reduction-identifier");
32155 resync_fail:
32156 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32157 /*or_comma=*/false,
32158 /*consume_paren=*/true);
32159 return list;
32160 }
32161 }
32162
32163 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32164 goto resync_fail;
32165
32166 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32167 NULL);
32168 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32169 {
32170 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32171 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32172 }
32173
32174 return nlist;
32175}
32176
32177/* OpenMP 2.5:
32178 schedule ( schedule-kind )
32179 schedule ( schedule-kind , expression )
32180
32181 schedule-kind:
32182 static | dynamic | guided | runtime | auto
32183
32184 OpenMP 4.5:
32185 schedule ( schedule-modifier : schedule-kind )
32186 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32187
32188 schedule-modifier:
32189 simd
32190 monotonic
32191 nonmonotonic */
32192
32193static tree
32194cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32195{
32196 tree c, t;
32197 int modifiers = 0, nmodifiers = 0;
32198
32199 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32200 return list;
32201
32202 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32203
32204 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32205 {
32206 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32207 const char *p = IDENTIFIER_POINTER (id);
32208 if (strcmp ("simd", p) == 0)
32209 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32210 else if (strcmp ("monotonic", p) == 0)
32211 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32212 else if (strcmp ("nonmonotonic", p) == 0)
32213 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32214 else
32215 break;
32216 cp_lexer_consume_token (parser->lexer);
32217 if (nmodifiers++ == 0
32218 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32219 cp_lexer_consume_token (parser->lexer);
32220 else
32221 {
32222 cp_parser_require (parser, CPP_COLON, RT_COLON);
32223 break;
32224 }
32225 }
32226
32227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32228 {
32229 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32230 const char *p = IDENTIFIER_POINTER (id);
32231
32232 switch (p[0])
32233 {
32234 case 'd':
32235 if (strcmp ("dynamic", p) != 0)
32236 goto invalid_kind;
32237 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32238 break;
32239
32240 case 'g':
32241 if (strcmp ("guided", p) != 0)
32242 goto invalid_kind;
32243 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32244 break;
32245
32246 case 'r':
32247 if (strcmp ("runtime", p) != 0)
32248 goto invalid_kind;
32249 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32250 break;
32251
32252 default:
32253 goto invalid_kind;
32254 }
32255 }
32256 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32257 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32258 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32259 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32260 else
32261 goto invalid_kind;
32262 cp_lexer_consume_token (parser->lexer);
32263
32264 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32265 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32266 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32267 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32268 {
32269 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32270 "specified");
32271 modifiers = 0;
32272 }
32273
32274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32275 {
32276 cp_token *token;
32277 cp_lexer_consume_token (parser->lexer);
32278
32279 token = cp_lexer_peek_token (parser->lexer);
32280 t = cp_parser_assignment_expression (parser);
32281
32282 if (t == error_mark_node)
32283 goto resync_fail;
32284 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32285 error_at (token->location, "schedule %<runtime%> does not take "
32286 "a %<chunk_size%> parameter");
32287 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32288 error_at (token->location, "schedule %<auto%> does not take "
32289 "a %<chunk_size%> parameter");
32290 else
32291 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32292
32293 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32294 goto resync_fail;
32295 }
32296 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32297 goto resync_fail;
32298
32299 OMP_CLAUSE_SCHEDULE_KIND (c)
32300 = (enum omp_clause_schedule_kind)
32301 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32302
32303 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32304 OMP_CLAUSE_CHAIN (c) = list;
32305 return c;
32306
32307 invalid_kind:
32308 cp_parser_error (parser, "invalid schedule kind");
32309 resync_fail:
32310 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32311 /*or_comma=*/false,
32312 /*consume_paren=*/true);
32313 return list;
32314}
32315
32316/* OpenMP 3.0:
32317 untied */
32318
32319static tree
32320cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32321 tree list, location_t location)
32322{
32323 tree c;
32324
32325 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32326
32327 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32328 OMP_CLAUSE_CHAIN (c) = list;
32329 return c;
32330}
32331
32332/* OpenMP 4.0:
32333 inbranch
32334 notinbranch */
32335
32336static tree
32337cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32338 tree list, location_t location)
32339{
32340 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32341 tree c = build_omp_clause (location, code);
32342 OMP_CLAUSE_CHAIN (c) = list;
32343 return c;
32344}
32345
32346/* OpenMP 4.0:
32347 parallel
32348 for
32349 sections
32350 taskgroup */
32351
32352static tree
32353cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32354 enum omp_clause_code code,
32355 tree list, location_t location)
32356{
32357 tree c = build_omp_clause (location, code);
32358 OMP_CLAUSE_CHAIN (c) = list;
32359 return c;
32360}
32361
32362/* OpenMP 4.5:
32363 nogroup */
32364
32365static tree
32366cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32367 tree list, location_t location)
32368{
32369 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32370 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32371 OMP_CLAUSE_CHAIN (c) = list;
32372 return c;
32373}
32374
32375/* OpenMP 4.5:
32376 simd
32377 threads */
32378
32379static tree
32380cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32381 enum omp_clause_code code,
32382 tree list, location_t location)
32383{
32384 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32385 tree c = build_omp_clause (location, code);
32386 OMP_CLAUSE_CHAIN (c) = list;
32387 return c;
32388}
32389
32390/* OpenMP 4.0:
32391 num_teams ( expression ) */
32392
32393static tree
32394cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32395 location_t location)
32396{
32397 tree t, c;
32398
32399 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32400 return list;
32401
32402 t = cp_parser_expression (parser);
32403
32404 if (t == error_mark_node
32405 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32406 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32407 /*or_comma=*/false,
32408 /*consume_paren=*/true);
32409
32410 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32411 "num_teams", location);
32412
32413 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32414 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32415 OMP_CLAUSE_CHAIN (c) = list;
32416
32417 return c;
32418}
32419
32420/* OpenMP 4.0:
32421 thread_limit ( expression ) */
32422
32423static tree
32424cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32425 location_t location)
32426{
32427 tree t, c;
32428
32429 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32430 return list;
32431
32432 t = cp_parser_expression (parser);
32433
32434 if (t == error_mark_node
32435 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32437 /*or_comma=*/false,
32438 /*consume_paren=*/true);
32439
32440 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32441 "thread_limit", location);
32442
32443 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32444 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32445 OMP_CLAUSE_CHAIN (c) = list;
32446
32447 return c;
32448}
32449
32450/* OpenMP 4.0:
32451 aligned ( variable-list )
32452 aligned ( variable-list : constant-expression ) */
32453
32454static tree
32455cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32456{
32457 tree nlist, c, alignment = NULL_TREE;
32458 bool colon;
32459
32460 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32461 return list;
32462
32463 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32464 &colon);
32465
32466 if (colon)
32467 {
32468 alignment = cp_parser_constant_expression (parser);
32469
32470 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32471 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32472 /*or_comma=*/false,
32473 /*consume_paren=*/true);
32474
32475 if (alignment == error_mark_node)
32476 alignment = NULL_TREE;
32477 }
32478
32479 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32480 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32481
32482 return nlist;
32483}
32484
32485/* OpenMP 4.0:
32486 linear ( variable-list )
32487 linear ( variable-list : expression )
32488
32489 OpenMP 4.5:
32490 linear ( modifier ( variable-list ) )
32491 linear ( modifier ( variable-list ) : expression ) */
32492
32493static tree
32494cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32495 bool is_cilk_simd_fn, bool declare_simd)
32496{
32497 tree nlist, c, step = integer_one_node;
32498 bool colon;
32499 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32500
32501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32502 return list;
32503
32504 if (!is_cilk_simd_fn
32505 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32506 {
32507 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32508 const char *p = IDENTIFIER_POINTER (id);
32509
32510 if (strcmp ("ref", p) == 0)
32511 kind = OMP_CLAUSE_LINEAR_REF;
32512 else if (strcmp ("val", p) == 0)
32513 kind = OMP_CLAUSE_LINEAR_VAL;
32514 else if (strcmp ("uval", p) == 0)
32515 kind = OMP_CLAUSE_LINEAR_UVAL;
32516 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32517 cp_lexer_consume_token (parser->lexer);
32518 else
32519 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32520 }
32521
32522 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32523 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32524 &colon);
32525 else
32526 {
32527 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32528 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32529 if (colon)
32530 cp_parser_require (parser, CPP_COLON, RT_COLON);
32531 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32533 /*or_comma=*/false,
32534 /*consume_paren=*/true);
32535 }
32536
32537 if (colon)
32538 {
32539 step = NULL_TREE;
32540 if (declare_simd
32541 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32542 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32543 {
32544 cp_token *token = cp_lexer_peek_token (parser->lexer);
32545 cp_parser_parse_tentatively (parser);
32546 step = cp_parser_id_expression (parser, /*template_p=*/false,
32547 /*check_dependency_p=*/true,
32548 /*template_p=*/NULL,
32549 /*declarator_p=*/false,
32550 /*optional_p=*/false);
32551 if (step != error_mark_node)
32552 step = cp_parser_lookup_name_simple (parser, step, token->location);
32553 if (step == error_mark_node)
32554 {
32555 step = NULL_TREE;
32556 cp_parser_abort_tentative_parse (parser);
32557 }
32558 else if (!cp_parser_parse_definitely (parser))
32559 step = NULL_TREE;
32560 }
32561 if (!step)
32562 step = cp_parser_expression (parser);
32563
32564 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32565 {
32566 sorry ("using parameters for %<linear%> step is not supported yet");
32567 step = integer_one_node;
32568 }
32569 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32571 /*or_comma=*/false,
32572 /*consume_paren=*/true);
32573
32574 if (step == error_mark_node)
32575 return list;
32576 }
32577
32578 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32579 {
32580 OMP_CLAUSE_LINEAR_STEP (c) = step;
32581 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32582 }
32583
32584 return nlist;
32585}
32586
32587/* OpenMP 4.0:
32588 safelen ( constant-expression ) */
32589
32590static tree
32591cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32592 location_t location)
32593{
32594 tree t, c;
32595
32596 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32597 return list;
32598
32599 t = cp_parser_constant_expression (parser);
32600
32601 if (t == error_mark_node
32602 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32603 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32604 /*or_comma=*/false,
32605 /*consume_paren=*/true);
32606
32607 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32608
32609 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32610 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32611 OMP_CLAUSE_CHAIN (c) = list;
32612
32613 return c;
32614}
32615
32616/* OpenMP 4.0:
32617 simdlen ( constant-expression ) */
32618
32619static tree
32620cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32621 location_t location)
32622{
32623 tree t, c;
32624
32625 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32626 return list;
32627
32628 t = cp_parser_constant_expression (parser);
32629
32630 if (t == error_mark_node
32631 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32633 /*or_comma=*/false,
32634 /*consume_paren=*/true);
32635
32636 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32637
32638 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32639 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32640 OMP_CLAUSE_CHAIN (c) = list;
32641
32642 return c;
32643}
32644
32645/* OpenMP 4.5:
32646 vec:
32647 identifier [+/- integer]
32648 vec , identifier [+/- integer]
32649*/
32650
32651static tree
32652cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32653 tree list)
32654{
32655 tree vec = NULL;
32656
32657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32658 {
32659 cp_parser_error (parser, "expected identifier");
32660 return list;
32661 }
32662
32663 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32664 {
32665 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32666 tree t, identifier = cp_parser_identifier (parser);
32667 tree addend = NULL;
32668
32669 if (identifier == error_mark_node)
32670 t = error_mark_node;
32671 else
32672 {
32673 t = cp_parser_lookup_name_simple
32674 (parser, identifier,
32675 cp_lexer_peek_token (parser->lexer)->location);
32676 if (t == error_mark_node)
32677 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32678 id_loc);
32679 }
32680
32681 bool neg = false;
32682 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32683 neg = true;
32684 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32685 {
32686 addend = integer_zero_node;
32687 goto add_to_vector;
32688 }
32689 cp_lexer_consume_token (parser->lexer);
32690
32691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32692 {
32693 cp_parser_error (parser, "expected integer");
32694 return list;
32695 }
32696
32697 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32698 if (TREE_CODE (addend) != INTEGER_CST)
32699 {
32700 cp_parser_error (parser, "expected integer");
32701 return list;
32702 }
32703 cp_lexer_consume_token (parser->lexer);
32704
32705 add_to_vector:
32706 if (t != error_mark_node)
32707 {
32708 vec = tree_cons (addend, t, vec);
32709 if (neg)
32710 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32711 }
32712
32713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32714 break;
32715
32716 cp_lexer_consume_token (parser->lexer);
32717 }
32718
32719 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32720 {
32721 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32722 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32723 OMP_CLAUSE_DECL (u) = nreverse (vec);
32724 OMP_CLAUSE_CHAIN (u) = list;
32725 return u;
32726 }
32727 return list;
32728}
32729
32730/* OpenMP 4.0:
32731 depend ( depend-kind : variable-list )
32732
32733 depend-kind:
32734 in | out | inout
32735
32736 OpenMP 4.5:
32737 depend ( source )
32738
32739 depend ( sink : vec ) */
32740
32741static tree
32742cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32743{
32744 tree nlist, c;
32745 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32746
32747 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32748 return list;
32749
32750 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32751 {
32752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32753 const char *p = IDENTIFIER_POINTER (id);
32754
32755 if (strcmp ("in", p) == 0)
32756 kind = OMP_CLAUSE_DEPEND_IN;
32757 else if (strcmp ("inout", p) == 0)
32758 kind = OMP_CLAUSE_DEPEND_INOUT;
32759 else if (strcmp ("out", p) == 0)
32760 kind = OMP_CLAUSE_DEPEND_OUT;
32761 else if (strcmp ("source", p) == 0)
32762 kind = OMP_CLAUSE_DEPEND_SOURCE;
32763 else if (strcmp ("sink", p) == 0)
32764 kind = OMP_CLAUSE_DEPEND_SINK;
32765 else
32766 goto invalid_kind;
32767 }
32768 else
32769 goto invalid_kind;
32770
32771 cp_lexer_consume_token (parser->lexer);
32772
32773 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32774 {
32775 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32776 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32777 OMP_CLAUSE_DECL (c) = NULL_TREE;
32778 OMP_CLAUSE_CHAIN (c) = list;
32779 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32780 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32781 /*or_comma=*/false,
32782 /*consume_paren=*/true);
32783 return c;
32784 }
32785
32786 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32787 goto resync_fail;
32788
32789 if (kind == OMP_CLAUSE_DEPEND_SINK)
32790 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32791 else
32792 {
32793 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32794 list, NULL);
32795
32796 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32797 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32798 }
32799 return nlist;
32800
32801 invalid_kind:
32802 cp_parser_error (parser, "invalid depend kind");
32803 resync_fail:
32804 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32805 /*or_comma=*/false,
32806 /*consume_paren=*/true);
32807 return list;
32808}
32809
32810/* OpenMP 4.0:
32811 map ( map-kind : variable-list )
32812 map ( variable-list )
32813
32814 map-kind:
32815 alloc | to | from | tofrom
32816
32817 OpenMP 4.5:
32818 map-kind:
32819 alloc | to | from | tofrom | release | delete
32820
32821 map ( always [,] map-kind: variable-list ) */
32822
32823static tree
32824cp_parser_omp_clause_map (cp_parser *parser, tree list)
32825{
32826 tree nlist, c;
32827 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32828 bool always = false;
32829
32830 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32831 return list;
32832
32833 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32834 {
32835 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32836 const char *p = IDENTIFIER_POINTER (id);
32837
32838 if (strcmp ("always", p) == 0)
32839 {
32840 int nth = 2;
32841 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32842 nth++;
32843 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32844 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32845 == RID_DELETE))
32846 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32847 == CPP_COLON))
32848 {
32849 always = true;
32850 cp_lexer_consume_token (parser->lexer);
32851 if (nth == 3)
32852 cp_lexer_consume_token (parser->lexer);
32853 }
32854 }
32855 }
32856
32857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32858 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32859 {
32860 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32861 const char *p = IDENTIFIER_POINTER (id);
32862
32863 if (strcmp ("alloc", p) == 0)
32864 kind = GOMP_MAP_ALLOC;
32865 else if (strcmp ("to", p) == 0)
32866 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32867 else if (strcmp ("from", p) == 0)
32868 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32869 else if (strcmp ("tofrom", p) == 0)
32870 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32871 else if (strcmp ("release", p) == 0)
32872 kind = GOMP_MAP_RELEASE;
32873 else
32874 {
32875 cp_parser_error (parser, "invalid map kind");
32876 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32877 /*or_comma=*/false,
32878 /*consume_paren=*/true);
32879 return list;
32880 }
32881 cp_lexer_consume_token (parser->lexer);
32882 cp_lexer_consume_token (parser->lexer);
32883 }
32884 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32885 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32886 {
32887 kind = GOMP_MAP_DELETE;
32888 cp_lexer_consume_token (parser->lexer);
32889 cp_lexer_consume_token (parser->lexer);
32890 }
32891
32892 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32893 NULL);
32894
32895 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32896 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32897
32898 return nlist;
32899}
32900
32901/* OpenMP 4.0:
32902 device ( expression ) */
32903
32904static tree
32905cp_parser_omp_clause_device (cp_parser *parser, tree list,
32906 location_t location)
32907{
32908 tree t, c;
32909
32910 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32911 return list;
32912
32913 t = cp_parser_expression (parser);
32914
32915 if (t == error_mark_node
32916 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32918 /*or_comma=*/false,
32919 /*consume_paren=*/true);
32920
32921 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32922 "device", location);
32923
32924 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32925 OMP_CLAUSE_DEVICE_ID (c) = t;
32926 OMP_CLAUSE_CHAIN (c) = list;
32927
32928 return c;
32929}
32930
32931/* OpenMP 4.0:
32932 dist_schedule ( static )
32933 dist_schedule ( static , expression ) */
32934
32935static tree
32936cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32937 location_t location)
32938{
32939 tree c, t;
32940
32941 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32942 return list;
32943
32944 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32945
32946 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32947 goto invalid_kind;
32948 cp_lexer_consume_token (parser->lexer);
32949
32950 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32951 {
32952 cp_lexer_consume_token (parser->lexer);
32953
32954 t = cp_parser_assignment_expression (parser);
32955
32956 if (t == error_mark_node)
32957 goto resync_fail;
32958 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32959
32960 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32961 goto resync_fail;
32962 }
32963 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32964 goto resync_fail;
32965
32966 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32967 location);
32968 OMP_CLAUSE_CHAIN (c) = list;
32969 return c;
32970
32971 invalid_kind:
32972 cp_parser_error (parser, "invalid dist_schedule kind");
32973 resync_fail:
32974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32975 /*or_comma=*/false,
32976 /*consume_paren=*/true);
32977 return list;
32978}
32979
32980/* OpenMP 4.0:
32981 proc_bind ( proc-bind-kind )
32982
32983 proc-bind-kind:
32984 master | close | spread */
32985
32986static tree
32987cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32988 location_t location)
32989{
32990 tree c;
32991 enum omp_clause_proc_bind_kind kind;
32992
32993 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32994 return list;
32995
32996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32997 {
32998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32999 const char *p = IDENTIFIER_POINTER (id);
33000
33001 if (strcmp ("master", p) == 0)
33002 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33003 else if (strcmp ("close", p) == 0)
33004 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33005 else if (strcmp ("spread", p) == 0)
33006 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33007 else
33008 goto invalid_kind;
33009 }
33010 else
33011 goto invalid_kind;
33012
33013 cp_lexer_consume_token (parser->lexer);
33014 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33015 goto resync_fail;
33016
33017 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33018 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33019 location);
33020 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33021 OMP_CLAUSE_CHAIN (c) = list;
33022 return c;
33023
33024 invalid_kind:
33025 cp_parser_error (parser, "invalid depend kind");
33026 resync_fail:
33027 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33028 /*or_comma=*/false,
33029 /*consume_paren=*/true);
33030 return list;
33031}
33032
33033/* OpenACC:
33034 async [( int-expr )] */
33035
33036static tree
33037cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33038{
33039 tree c, t;
33040 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33041
33042 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33043
33044 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33045 {
33046 cp_lexer_consume_token (parser->lexer);
33047
33048 t = cp_parser_expression (parser);
33049 if (t == error_mark_node
33050 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33052 /*or_comma=*/false,
33053 /*consume_paren=*/true);
33054 }
33055
33056 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33057
33058 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33059 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33060 OMP_CLAUSE_CHAIN (c) = list;
33061 list = c;
33062
33063 return list;
33064}
33065
33066/* Parse all OpenACC clauses. The set clauses allowed by the directive
33067 is a bitmask in MASK. Return the list of clauses found. */
33068
33069static tree
33070cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33071 const char *where, cp_token *pragma_tok,
33072 bool finish_p = true)
33073{
33074 tree clauses = NULL;
33075 bool first = true;
33076
33077 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33078 {
33079 location_t here;
33080 pragma_omp_clause c_kind;
33081 omp_clause_code code;
33082 const char *c_name;
33083 tree prev = clauses;
33084
33085 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33086 cp_lexer_consume_token (parser->lexer);
33087
33088 here = cp_lexer_peek_token (parser->lexer)->location;
33089 c_kind = cp_parser_omp_clause_name (parser);
33090
33091 switch (c_kind)
33092 {
33093 case PRAGMA_OACC_CLAUSE_ASYNC:
33094 clauses = cp_parser_oacc_clause_async (parser, clauses);
33095 c_name = "async";
33096 break;
33097 case PRAGMA_OACC_CLAUSE_AUTO:
33098 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33099 clauses, here);
33100 c_name = "auto";
33101 break;
33102 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33103 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33104 c_name = "collapse";
33105 break;
33106 case PRAGMA_OACC_CLAUSE_COPY:
33107 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33108 c_name = "copy";
33109 break;
33110 case PRAGMA_OACC_CLAUSE_COPYIN:
33111 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33112 c_name = "copyin";
33113 break;
33114 case PRAGMA_OACC_CLAUSE_COPYOUT:
33115 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33116 c_name = "copyout";
33117 break;
33118 case PRAGMA_OACC_CLAUSE_CREATE:
33119 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33120 c_name = "create";
33121 break;
33122 case PRAGMA_OACC_CLAUSE_DELETE:
33123 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33124 c_name = "delete";
33125 break;
33126 case PRAGMA_OMP_CLAUSE_DEFAULT:
33127 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33128 c_name = "default";
33129 break;
33130 case PRAGMA_OACC_CLAUSE_DEVICE:
33131 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33132 c_name = "device";
33133 break;
33134 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33135 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33136 c_name = "deviceptr";
33137 break;
33138 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33139 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33140 c_name = "device_resident";
33141 break;
33142 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33143 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33144 clauses);
33145 c_name = "firstprivate";
33146 break;
33147 case PRAGMA_OACC_CLAUSE_GANG:
33148 c_name = "gang";
33149 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33150 c_name, clauses);
33151 break;
33152 case PRAGMA_OACC_CLAUSE_HOST:
33153 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33154 c_name = "host";
33155 break;
33156 case PRAGMA_OACC_CLAUSE_IF:
33157 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33158 c_name = "if";
33159 break;
33160 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33161 clauses = cp_parser_oacc_simple_clause (parser,
33162 OMP_CLAUSE_INDEPENDENT,
33163 clauses, here);
33164 c_name = "independent";
33165 break;
33166 case PRAGMA_OACC_CLAUSE_LINK:
33167 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33168 c_name = "link";
33169 break;
33170 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33171 code = OMP_CLAUSE_NUM_GANGS;
33172 c_name = "num_gangs";
33173 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33174 clauses);
33175 break;
33176 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33177 c_name = "num_workers";
33178 code = OMP_CLAUSE_NUM_WORKERS;
33179 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33180 clauses);
33181 break;
33182 case PRAGMA_OACC_CLAUSE_PRESENT:
33183 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33184 c_name = "present";
33185 break;
33186 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33187 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33188 c_name = "present_or_copy";
33189 break;
33190 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33191 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33192 c_name = "present_or_copyin";
33193 break;
33194 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33195 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33196 c_name = "present_or_copyout";
33197 break;
33198 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33199 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33200 c_name = "present_or_create";
33201 break;
33202 case PRAGMA_OACC_CLAUSE_PRIVATE:
33203 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33204 clauses);
33205 c_name = "private";
33206 break;
33207 case PRAGMA_OACC_CLAUSE_REDUCTION:
33208 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33209 c_name = "reduction";
33210 break;
33211 case PRAGMA_OACC_CLAUSE_SELF:
33212 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33213 c_name = "self";
33214 break;
33215 case PRAGMA_OACC_CLAUSE_SEQ:
33216 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33217 clauses, here);
33218 c_name = "seq";
33219 break;
33220 case PRAGMA_OACC_CLAUSE_TILE:
33221 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33222 c_name = "tile";
33223 break;
33224 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33225 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33226 clauses);
33227 c_name = "use_device";
33228 break;
33229 case PRAGMA_OACC_CLAUSE_VECTOR:
33230 c_name = "vector";
33231 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33232 c_name, clauses);
33233 break;
33234 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33235 c_name = "vector_length";
33236 code = OMP_CLAUSE_VECTOR_LENGTH;
33237 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33238 clauses);
33239 break;
33240 case PRAGMA_OACC_CLAUSE_WAIT:
33241 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33242 c_name = "wait";
33243 break;
33244 case PRAGMA_OACC_CLAUSE_WORKER:
33245 c_name = "worker";
33246 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33247 c_name, clauses);
33248 break;
33249 default:
33250 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33251 goto saw_error;
33252 }
33253
33254 first = false;
33255
33256 if (((mask >> c_kind) & 1) == 0)
33257 {
33258 /* Remove the invalid clause(s) from the list to avoid
33259 confusing the rest of the compiler. */
33260 clauses = prev;
33261 error_at (here, "%qs is not valid for %qs", c_name, where);
33262 }
33263 }
33264
33265 saw_error:
33266 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33267
33268 if (finish_p)
33269 return finish_omp_clauses (clauses, C_ORT_ACC);
33270
33271 return clauses;
33272}
33273
33274/* Parse all OpenMP clauses. The set clauses allowed by the directive
33275 is a bitmask in MASK. Return the list of clauses found; the result
33276 of clause default goes in *pdefault. */
33277
33278static tree
33279cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33280 const char *where, cp_token *pragma_tok,
33281 bool finish_p = true)
33282{
33283 tree clauses = NULL;
33284 bool first = true;
33285 cp_token *token = NULL;
33286
33287 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33288 {
33289 pragma_omp_clause c_kind;
33290 const char *c_name;
33291 tree prev = clauses;
33292
33293 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33294 cp_lexer_consume_token (parser->lexer);
33295
33296 token = cp_lexer_peek_token (parser->lexer);
33297 c_kind = cp_parser_omp_clause_name (parser);
33298
33299 switch (c_kind)
33300 {
33301 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33302 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33303 token->location);
33304 c_name = "collapse";
33305 break;
33306 case PRAGMA_OMP_CLAUSE_COPYIN:
33307 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33308 c_name = "copyin";
33309 break;
33310 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33311 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33312 clauses);
33313 c_name = "copyprivate";
33314 break;
33315 case PRAGMA_OMP_CLAUSE_DEFAULT:
33316 clauses = cp_parser_omp_clause_default (parser, clauses,
33317 token->location, false);
33318 c_name = "default";
33319 break;
33320 case PRAGMA_OMP_CLAUSE_FINAL:
33321 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33322 c_name = "final";
33323 break;
33324 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33325 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33326 clauses);
33327 c_name = "firstprivate";
33328 break;
33329 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33330 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33331 token->location);
33332 c_name = "grainsize";
33333 break;
33334 case PRAGMA_OMP_CLAUSE_HINT:
33335 clauses = cp_parser_omp_clause_hint (parser, clauses,
33336 token->location);
33337 c_name = "hint";
33338 break;
33339 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33340 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33341 token->location);
33342 c_name = "defaultmap";
33343 break;
33344 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33345 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33346 clauses);
33347 c_name = "use_device_ptr";
33348 break;
33349 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33350 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33351 clauses);
33352 c_name = "is_device_ptr";
33353 break;
33354 case PRAGMA_OMP_CLAUSE_IF:
33355 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33356 true);
33357 c_name = "if";
33358 break;
33359 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33360 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33361 clauses);
33362 c_name = "lastprivate";
33363 break;
33364 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33365 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33366 token->location);
33367 c_name = "mergeable";
33368 break;
33369 case PRAGMA_OMP_CLAUSE_NOWAIT:
33370 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33371 c_name = "nowait";
33372 break;
33373 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33374 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33375 token->location);
33376 c_name = "num_tasks";
33377 break;
33378 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33379 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33380 token->location);
33381 c_name = "num_threads";
33382 break;
33383 case PRAGMA_OMP_CLAUSE_ORDERED:
33384 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33385 token->location);
33386 c_name = "ordered";
33387 break;
33388 case PRAGMA_OMP_CLAUSE_PRIORITY:
33389 clauses = cp_parser_omp_clause_priority (parser, clauses,
33390 token->location);
33391 c_name = "priority";
33392 break;
33393 case PRAGMA_OMP_CLAUSE_PRIVATE:
33394 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33395 clauses);
33396 c_name = "private";
33397 break;
33398 case PRAGMA_OMP_CLAUSE_REDUCTION:
33399 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33400 c_name = "reduction";
33401 break;
33402 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33403 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33404 token->location);
33405 c_name = "schedule";
33406 break;
33407 case PRAGMA_OMP_CLAUSE_SHARED:
33408 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33409 clauses);
33410 c_name = "shared";
33411 break;
33412 case PRAGMA_OMP_CLAUSE_UNTIED:
33413 clauses = cp_parser_omp_clause_untied (parser, clauses,
33414 token->location);
33415 c_name = "untied";
33416 break;
33417 case PRAGMA_OMP_CLAUSE_INBRANCH:
33418 case PRAGMA_CILK_CLAUSE_MASK:
33419 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33420 clauses, token->location);
33421 c_name = "inbranch";
33422 break;
33423 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33424 case PRAGMA_CILK_CLAUSE_NOMASK:
33425 clauses = cp_parser_omp_clause_branch (parser,
33426 OMP_CLAUSE_NOTINBRANCH,
33427 clauses, token->location);
33428 c_name = "notinbranch";
33429 break;
33430 case PRAGMA_OMP_CLAUSE_PARALLEL:
33431 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33432 clauses, token->location);
33433 c_name = "parallel";
33434 if (!first)
33435 {
33436 clause_not_first:
33437 error_at (token->location, "%qs must be the first clause of %qs",
33438 c_name, where);
33439 clauses = prev;
33440 }
33441 break;
33442 case PRAGMA_OMP_CLAUSE_FOR:
33443 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33444 clauses, token->location);
33445 c_name = "for";
33446 if (!first)
33447 goto clause_not_first;
33448 break;
33449 case PRAGMA_OMP_CLAUSE_SECTIONS:
33450 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33451 clauses, token->location);
33452 c_name = "sections";
33453 if (!first)
33454 goto clause_not_first;
33455 break;
33456 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33457 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33458 clauses, token->location);
33459 c_name = "taskgroup";
33460 if (!first)
33461 goto clause_not_first;
33462 break;
33463 case PRAGMA_OMP_CLAUSE_LINK:
33464 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33465 c_name = "to";
33466 break;
33467 case PRAGMA_OMP_CLAUSE_TO:
33468 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33469 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33470 clauses);
33471 else
33472 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33473 c_name = "to";
33474 break;
33475 case PRAGMA_OMP_CLAUSE_FROM:
33476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33477 c_name = "from";
33478 break;
33479 case PRAGMA_OMP_CLAUSE_UNIFORM:
33480 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33481 clauses);
33482 c_name = "uniform";
33483 break;
33484 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33485 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33486 token->location);
33487 c_name = "num_teams";
33488 break;
33489 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33490 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33491 token->location);
33492 c_name = "thread_limit";
33493 break;
33494 case PRAGMA_OMP_CLAUSE_ALIGNED:
33495 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33496 c_name = "aligned";
33497 break;
33498 case PRAGMA_OMP_CLAUSE_LINEAR:
33499 {
33500 bool cilk_simd_fn = false, declare_simd = false;
33501 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33502 cilk_simd_fn = true;
33503 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33504 declare_simd = true;
33505 clauses = cp_parser_omp_clause_linear (parser, clauses,
33506 cilk_simd_fn, declare_simd);
33507 }
33508 c_name = "linear";
33509 break;
33510 case PRAGMA_OMP_CLAUSE_DEPEND:
33511 clauses = cp_parser_omp_clause_depend (parser, clauses,
33512 token->location);
33513 c_name = "depend";
33514 break;
33515 case PRAGMA_OMP_CLAUSE_MAP:
33516 clauses = cp_parser_omp_clause_map (parser, clauses);
33517 c_name = "map";
33518 break;
33519 case PRAGMA_OMP_CLAUSE_DEVICE:
33520 clauses = cp_parser_omp_clause_device (parser, clauses,
33521 token->location);
33522 c_name = "device";
33523 break;
33524 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33525 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33526 token->location);
33527 c_name = "dist_schedule";
33528 break;
33529 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33530 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33531 token->location);
33532 c_name = "proc_bind";
33533 break;
33534 case PRAGMA_OMP_CLAUSE_SAFELEN:
33535 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33536 token->location);
33537 c_name = "safelen";
33538 break;
33539 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33540 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33541 token->location);
33542 c_name = "simdlen";
33543 break;
33544 case PRAGMA_OMP_CLAUSE_NOGROUP:
33545 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33546 token->location);
33547 c_name = "nogroup";
33548 break;
33549 case PRAGMA_OMP_CLAUSE_THREADS:
33550 clauses
33551 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33552 clauses, token->location);
33553 c_name = "threads";
33554 break;
33555 case PRAGMA_OMP_CLAUSE_SIMD:
33556 clauses
33557 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33558 clauses, token->location);
33559 c_name = "simd";
33560 break;
33561 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33562 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33563 c_name = "simdlen";
33564 break;
33565 default:
33566 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33567 goto saw_error;
33568 }
33569
33570 first = false;
33571
33572 if (((mask >> c_kind) & 1) == 0)
33573 {
33574 /* Remove the invalid clause(s) from the list to avoid
33575 confusing the rest of the compiler. */
33576 clauses = prev;
33577 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33578 }
33579 }
33580 saw_error:
33581 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33582 no reason to skip to the end. */
33583 if (!(flag_cilkplus && pragma_tok == NULL))
33584 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33585 if (finish_p)
33586 {
33587 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33588 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33589 else
33590 return finish_omp_clauses (clauses, C_ORT_OMP);
33591 }
33592 return clauses;
33593}
33594
33595/* OpenMP 2.5:
33596 structured-block:
33597 statement
33598
33599 In practice, we're also interested in adding the statement to an
33600 outer node. So it is convenient if we work around the fact that
33601 cp_parser_statement calls add_stmt. */
33602
33603static unsigned
33604cp_parser_begin_omp_structured_block (cp_parser *parser)
33605{
33606 unsigned save = parser->in_statement;
33607
33608 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33609 This preserves the "not within loop or switch" style error messages
33610 for nonsense cases like
33611 void foo() {
33612 #pragma omp single
33613 break;
33614 }
33615 */
33616 if (parser->in_statement)
33617 parser->in_statement = IN_OMP_BLOCK;
33618
33619 return save;
33620}
33621
33622static void
33623cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33624{
33625 parser->in_statement = save;
33626}
33627
33628static tree
33629cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33630{
33631 tree stmt = begin_omp_structured_block ();
33632 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33633
33634 cp_parser_statement (parser, NULL_TREE, false, if_p);
33635
33636 cp_parser_end_omp_structured_block (parser, save);
33637 return finish_omp_structured_block (stmt);
33638}
33639
33640/* OpenMP 2.5:
33641 # pragma omp atomic new-line
33642 expression-stmt
33643
33644 expression-stmt:
33645 x binop= expr | x++ | ++x | x-- | --x
33646 binop:
33647 +, *, -, /, &, ^, |, <<, >>
33648
33649 where x is an lvalue expression with scalar type.
33650
33651 OpenMP 3.1:
33652 # pragma omp atomic new-line
33653 update-stmt
33654
33655 # pragma omp atomic read new-line
33656 read-stmt
33657
33658 # pragma omp atomic write new-line
33659 write-stmt
33660
33661 # pragma omp atomic update new-line
33662 update-stmt
33663
33664 # pragma omp atomic capture new-line
33665 capture-stmt
33666
33667 # pragma omp atomic capture new-line
33668 capture-block
33669
33670 read-stmt:
33671 v = x
33672 write-stmt:
33673 x = expr
33674 update-stmt:
33675 expression-stmt | x = x binop expr
33676 capture-stmt:
33677 v = expression-stmt
33678 capture-block:
33679 { v = x; update-stmt; } | { update-stmt; v = x; }
33680
33681 OpenMP 4.0:
33682 update-stmt:
33683 expression-stmt | x = x binop expr | x = expr binop x
33684 capture-stmt:
33685 v = update-stmt
33686 capture-block:
33687 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33688
33689 where x and v are lvalue expressions with scalar type. */
33690
33691static void
33692cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33693{
33694 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33695 tree rhs1 = NULL_TREE, orig_lhs;
33696 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33697 bool structured_block = false;
33698 bool seq_cst = false;
33699
33700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33701 {
33702 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33703 const char *p = IDENTIFIER_POINTER (id);
33704
33705 if (!strcmp (p, "seq_cst"))
33706 {
33707 seq_cst = true;
33708 cp_lexer_consume_token (parser->lexer);
33709 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33710 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33711 cp_lexer_consume_token (parser->lexer);
33712 }
33713 }
33714 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33715 {
33716 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33717 const char *p = IDENTIFIER_POINTER (id);
33718
33719 if (!strcmp (p, "read"))
33720 code = OMP_ATOMIC_READ;
33721 else if (!strcmp (p, "write"))
33722 code = NOP_EXPR;
33723 else if (!strcmp (p, "update"))
33724 code = OMP_ATOMIC;
33725 else if (!strcmp (p, "capture"))
33726 code = OMP_ATOMIC_CAPTURE_NEW;
33727 else
33728 p = NULL;
33729 if (p)
33730 cp_lexer_consume_token (parser->lexer);
33731 }
33732 if (!seq_cst)
33733 {
33734 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33735 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33736 cp_lexer_consume_token (parser->lexer);
33737
33738 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33739 {
33740 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33741 const char *p = IDENTIFIER_POINTER (id);
33742
33743 if (!strcmp (p, "seq_cst"))
33744 {
33745 seq_cst = true;
33746 cp_lexer_consume_token (parser->lexer);
33747 }
33748 }
33749 }
33750 cp_parser_require_pragma_eol (parser, pragma_tok);
33751
33752 switch (code)
33753 {
33754 case OMP_ATOMIC_READ:
33755 case NOP_EXPR: /* atomic write */
33756 v = cp_parser_unary_expression (parser);
33757 if (v == error_mark_node)
33758 goto saw_error;
33759 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33760 goto saw_error;
33761 if (code == NOP_EXPR)
33762 lhs = cp_parser_expression (parser);
33763 else
33764 lhs = cp_parser_unary_expression (parser);
33765 if (lhs == error_mark_node)
33766 goto saw_error;
33767 if (code == NOP_EXPR)
33768 {
33769 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33770 opcode. */
33771 code = OMP_ATOMIC;
33772 rhs = lhs;
33773 lhs = v;
33774 v = NULL_TREE;
33775 }
33776 goto done;
33777 case OMP_ATOMIC_CAPTURE_NEW:
33778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33779 {
33780 cp_lexer_consume_token (parser->lexer);
33781 structured_block = true;
33782 }
33783 else
33784 {
33785 v = cp_parser_unary_expression (parser);
33786 if (v == error_mark_node)
33787 goto saw_error;
33788 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33789 goto saw_error;
33790 }
33791 default:
33792 break;
33793 }
33794
33795restart:
33796 lhs = cp_parser_unary_expression (parser);
33797 orig_lhs = lhs;
33798 switch (TREE_CODE (lhs))
33799 {
33800 case ERROR_MARK:
33801 goto saw_error;
33802
33803 case POSTINCREMENT_EXPR:
33804 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33805 code = OMP_ATOMIC_CAPTURE_OLD;
33806 /* FALLTHROUGH */
33807 case PREINCREMENT_EXPR:
33808 lhs = TREE_OPERAND (lhs, 0);
33809 opcode = PLUS_EXPR;
33810 rhs = integer_one_node;
33811 break;
33812
33813 case POSTDECREMENT_EXPR:
33814 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33815 code = OMP_ATOMIC_CAPTURE_OLD;
33816 /* FALLTHROUGH */
33817 case PREDECREMENT_EXPR:
33818 lhs = TREE_OPERAND (lhs, 0);
33819 opcode = MINUS_EXPR;
33820 rhs = integer_one_node;
33821 break;
33822
33823 case COMPOUND_EXPR:
33824 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33825 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33826 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33827 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33828 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33829 (TREE_OPERAND (lhs, 1), 0), 0)))
33830 == BOOLEAN_TYPE)
33831 /* Undo effects of boolean_increment for post {in,de}crement. */
33832 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33833 /* FALLTHRU */
33834 case MODIFY_EXPR:
33835 if (TREE_CODE (lhs) == MODIFY_EXPR
33836 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33837 {
33838 /* Undo effects of boolean_increment. */
33839 if (integer_onep (TREE_OPERAND (lhs, 1)))
33840 {
33841 /* This is pre or post increment. */
33842 rhs = TREE_OPERAND (lhs, 1);
33843 lhs = TREE_OPERAND (lhs, 0);
33844 opcode = NOP_EXPR;
33845 if (code == OMP_ATOMIC_CAPTURE_NEW
33846 && !structured_block
33847 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33848 code = OMP_ATOMIC_CAPTURE_OLD;
33849 break;
33850 }
33851 }
33852 /* FALLTHRU */
33853 default:
33854 switch (cp_lexer_peek_token (parser->lexer)->type)
33855 {
33856 case CPP_MULT_EQ:
33857 opcode = MULT_EXPR;
33858 break;
33859 case CPP_DIV_EQ:
33860 opcode = TRUNC_DIV_EXPR;
33861 break;
33862 case CPP_PLUS_EQ:
33863 opcode = PLUS_EXPR;
33864 break;
33865 case CPP_MINUS_EQ:
33866 opcode = MINUS_EXPR;
33867 break;
33868 case CPP_LSHIFT_EQ:
33869 opcode = LSHIFT_EXPR;
33870 break;
33871 case CPP_RSHIFT_EQ:
33872 opcode = RSHIFT_EXPR;
33873 break;
33874 case CPP_AND_EQ:
33875 opcode = BIT_AND_EXPR;
33876 break;
33877 case CPP_OR_EQ:
33878 opcode = BIT_IOR_EXPR;
33879 break;
33880 case CPP_XOR_EQ:
33881 opcode = BIT_XOR_EXPR;
33882 break;
33883 case CPP_EQ:
33884 enum cp_parser_prec oprec;
33885 cp_token *token;
33886 cp_lexer_consume_token (parser->lexer);
33887 cp_parser_parse_tentatively (parser);
33888 rhs1 = cp_parser_simple_cast_expression (parser);
33889 if (rhs1 == error_mark_node)
33890 {
33891 cp_parser_abort_tentative_parse (parser);
33892 cp_parser_simple_cast_expression (parser);
33893 goto saw_error;
33894 }
33895 token = cp_lexer_peek_token (parser->lexer);
33896 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33897 {
33898 cp_parser_abort_tentative_parse (parser);
33899 cp_parser_parse_tentatively (parser);
33900 rhs = cp_parser_binary_expression (parser, false, true,
33901 PREC_NOT_OPERATOR, NULL);
33902 if (rhs == error_mark_node)
33903 {
33904 cp_parser_abort_tentative_parse (parser);
33905 cp_parser_binary_expression (parser, false, true,
33906 PREC_NOT_OPERATOR, NULL);
33907 goto saw_error;
33908 }
33909 switch (TREE_CODE (rhs))
33910 {
33911 case MULT_EXPR:
33912 case TRUNC_DIV_EXPR:
33913 case RDIV_EXPR:
33914 case PLUS_EXPR:
33915 case MINUS_EXPR:
33916 case LSHIFT_EXPR:
33917 case RSHIFT_EXPR:
33918 case BIT_AND_EXPR:
33919 case BIT_IOR_EXPR:
33920 case BIT_XOR_EXPR:
33921 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33922 {
33923 if (cp_parser_parse_definitely (parser))
33924 {
33925 opcode = TREE_CODE (rhs);
33926 rhs1 = TREE_OPERAND (rhs, 0);
33927 rhs = TREE_OPERAND (rhs, 1);
33928 goto stmt_done;
33929 }
33930 else
33931 goto saw_error;
33932 }
33933 break;
33934 default:
33935 break;
33936 }
33937 cp_parser_abort_tentative_parse (parser);
33938 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33939 {
33940 rhs = cp_parser_expression (parser);
33941 if (rhs == error_mark_node)
33942 goto saw_error;
33943 opcode = NOP_EXPR;
33944 rhs1 = NULL_TREE;
33945 goto stmt_done;
33946 }
33947 cp_parser_error (parser,
33948 "invalid form of %<#pragma omp atomic%>");
33949 goto saw_error;
33950 }
33951 if (!cp_parser_parse_definitely (parser))
33952 goto saw_error;
33953 switch (token->type)
33954 {
33955 case CPP_SEMICOLON:
33956 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33957 {
33958 code = OMP_ATOMIC_CAPTURE_OLD;
33959 v = lhs;
33960 lhs = NULL_TREE;
33961 lhs1 = rhs1;
33962 rhs1 = NULL_TREE;
33963 cp_lexer_consume_token (parser->lexer);
33964 goto restart;
33965 }
33966 else if (structured_block)
33967 {
33968 opcode = NOP_EXPR;
33969 rhs = rhs1;
33970 rhs1 = NULL_TREE;
33971 goto stmt_done;
33972 }
33973 cp_parser_error (parser,
33974 "invalid form of %<#pragma omp atomic%>");
33975 goto saw_error;
33976 case CPP_MULT:
33977 opcode = MULT_EXPR;
33978 break;
33979 case CPP_DIV:
33980 opcode = TRUNC_DIV_EXPR;
33981 break;
33982 case CPP_PLUS:
33983 opcode = PLUS_EXPR;
33984 break;
33985 case CPP_MINUS:
33986 opcode = MINUS_EXPR;
33987 break;
33988 case CPP_LSHIFT:
33989 opcode = LSHIFT_EXPR;
33990 break;
33991 case CPP_RSHIFT:
33992 opcode = RSHIFT_EXPR;
33993 break;
33994 case CPP_AND:
33995 opcode = BIT_AND_EXPR;
33996 break;
33997 case CPP_OR:
33998 opcode = BIT_IOR_EXPR;
33999 break;
34000 case CPP_XOR:
34001 opcode = BIT_XOR_EXPR;
34002 break;
34003 default:
34004 cp_parser_error (parser,
34005 "invalid operator for %<#pragma omp atomic%>");
34006 goto saw_error;
34007 }
34008 oprec = TOKEN_PRECEDENCE (token);
34009 gcc_assert (oprec != PREC_NOT_OPERATOR);
34010 if (commutative_tree_code (opcode))
34011 oprec = (enum cp_parser_prec) (oprec - 1);
34012 cp_lexer_consume_token (parser->lexer);
34013 rhs = cp_parser_binary_expression (parser, false, false,
34014 oprec, NULL);
34015 if (rhs == error_mark_node)
34016 goto saw_error;
34017 goto stmt_done;
34018 /* FALLTHROUGH */
34019 default:
34020 cp_parser_error (parser,
34021 "invalid operator for %<#pragma omp atomic%>");
34022 goto saw_error;
34023 }
34024 cp_lexer_consume_token (parser->lexer);
34025
34026 rhs = cp_parser_expression (parser);
34027 if (rhs == error_mark_node)
34028 goto saw_error;
34029 break;
34030 }
34031stmt_done:
34032 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34033 {
34034 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34035 goto saw_error;
34036 v = cp_parser_unary_expression (parser);
34037 if (v == error_mark_node)
34038 goto saw_error;
34039 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34040 goto saw_error;
34041 lhs1 = cp_parser_unary_expression (parser);
34042 if (lhs1 == error_mark_node)
34043 goto saw_error;
34044 }
34045 if (structured_block)
34046 {
34047 cp_parser_consume_semicolon_at_end_of_statement (parser);
34048 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34049 }
34050done:
34051 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34052 if (!structured_block)
34053 cp_parser_consume_semicolon_at_end_of_statement (parser);
34054 return;
34055
34056 saw_error:
34057 cp_parser_skip_to_end_of_block_or_statement (parser);
34058 if (structured_block)
34059 {
34060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34061 cp_lexer_consume_token (parser->lexer);
34062 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34063 {
34064 cp_parser_skip_to_end_of_block_or_statement (parser);
34065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34066 cp_lexer_consume_token (parser->lexer);
34067 }
34068 }
34069}
34070
34071
34072/* OpenMP 2.5:
34073 # pragma omp barrier new-line */
34074
34075static void
34076cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34077{
34078 cp_parser_require_pragma_eol (parser, pragma_tok);
34079 finish_omp_barrier ();
34080}
34081
34082/* OpenMP 2.5:
34083 # pragma omp critical [(name)] new-line
34084 structured-block
34085
34086 OpenMP 4.5:
34087 # pragma omp critical [(name) [hint(expression)]] new-line
34088 structured-block */
34089
34090#define OMP_CRITICAL_CLAUSE_MASK \
34091 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34092
34093static tree
34094cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34095{
34096 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34097
34098 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34099 {
34100 cp_lexer_consume_token (parser->lexer);
34101
34102 name = cp_parser_identifier (parser);
34103
34104 if (name == error_mark_node
34105 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34107 /*or_comma=*/false,
34108 /*consume_paren=*/true);
34109 if (name == error_mark_node)
34110 name = NULL;
34111
34112 clauses = cp_parser_omp_all_clauses (parser,
34113 OMP_CRITICAL_CLAUSE_MASK,
34114 "#pragma omp critical", pragma_tok);
34115 }
34116 else
34117 cp_parser_require_pragma_eol (parser, pragma_tok);
34118
34119 stmt = cp_parser_omp_structured_block (parser, if_p);
34120 return c_finish_omp_critical (input_location, stmt, name, clauses);
34121}
34122
34123/* OpenMP 2.5:
34124 # pragma omp flush flush-vars[opt] new-line
34125
34126 flush-vars:
34127 ( variable-list ) */
34128
34129static void
34130cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34131{
34132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34133 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34134 cp_parser_require_pragma_eol (parser, pragma_tok);
34135
34136 finish_omp_flush ();
34137}
34138
34139/* Helper function, to parse omp for increment expression. */
34140
34141static tree
34142cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34143{
34144 tree cond = cp_parser_binary_expression (parser, false, true,
34145 PREC_NOT_OPERATOR, NULL);
34146 if (cond == error_mark_node
34147 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34148 {
34149 cp_parser_skip_to_end_of_statement (parser);
34150 return error_mark_node;
34151 }
34152
34153 switch (TREE_CODE (cond))
34154 {
34155 case GT_EXPR:
34156 case GE_EXPR:
34157 case LT_EXPR:
34158 case LE_EXPR:
34159 break;
34160 case NE_EXPR:
34161 if (code == CILK_SIMD || code == CILK_FOR)
34162 break;
34163 /* Fall through: OpenMP disallows NE_EXPR. */
34164 gcc_fallthrough ();
34165 default:
34166 return error_mark_node;
34167 }
34168
34169 /* If decl is an iterator, preserve LHS and RHS of the relational
34170 expr until finish_omp_for. */
34171 if (decl
34172 && (type_dependent_expression_p (decl)
34173 || CLASS_TYPE_P (TREE_TYPE (decl))))
34174 return cond;
34175
34176 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34177 TREE_CODE (cond),
34178 TREE_OPERAND (cond, 0), ERROR_MARK,
34179 TREE_OPERAND (cond, 1), ERROR_MARK,
34180 /*overload=*/NULL, tf_warning_or_error);
34181}
34182
34183/* Helper function, to parse omp for increment expression. */
34184
34185static tree
34186cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34187{
34188 cp_token *token = cp_lexer_peek_token (parser->lexer);
34189 enum tree_code op;
34190 tree lhs, rhs;
34191 cp_id_kind idk;
34192 bool decl_first;
34193
34194 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34195 {
34196 op = (token->type == CPP_PLUS_PLUS
34197 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34198 cp_lexer_consume_token (parser->lexer);
34199 lhs = cp_parser_simple_cast_expression (parser);
34200 if (lhs != decl
34201 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34202 return error_mark_node;
34203 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34204 }
34205
34206 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34207 if (lhs != decl
34208 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34209 return error_mark_node;
34210
34211 token = cp_lexer_peek_token (parser->lexer);
34212 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34213 {
34214 op = (token->type == CPP_PLUS_PLUS
34215 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34216 cp_lexer_consume_token (parser->lexer);
34217 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34218 }
34219
34220 op = cp_parser_assignment_operator_opt (parser);
34221 if (op == ERROR_MARK)
34222 return error_mark_node;
34223
34224 if (op != NOP_EXPR)
34225 {
34226 rhs = cp_parser_assignment_expression (parser);
34227 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34228 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34229 }
34230
34231 lhs = cp_parser_binary_expression (parser, false, false,
34232 PREC_ADDITIVE_EXPRESSION, NULL);
34233 token = cp_lexer_peek_token (parser->lexer);
34234 decl_first = (lhs == decl
34235 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34236 if (decl_first)
34237 lhs = NULL_TREE;
34238 if (token->type != CPP_PLUS
34239 && token->type != CPP_MINUS)
34240 return error_mark_node;
34241
34242 do
34243 {
34244 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34245 cp_lexer_consume_token (parser->lexer);
34246 rhs = cp_parser_binary_expression (parser, false, false,
34247 PREC_ADDITIVE_EXPRESSION, NULL);
34248 token = cp_lexer_peek_token (parser->lexer);
34249 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34250 {
34251 if (lhs == NULL_TREE)
34252 {
34253 if (op == PLUS_EXPR)
34254 lhs = rhs;
34255 else
34256 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34257 tf_warning_or_error);
34258 }
34259 else
34260 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34261 ERROR_MARK, NULL, tf_warning_or_error);
34262 }
34263 }
34264 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34265
34266 if (!decl_first)
34267 {
34268 if ((rhs != decl
34269 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34270 || op == MINUS_EXPR)
34271 return error_mark_node;
34272 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34273 }
34274 else
34275 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34276
34277 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34278}
34279
34280/* Parse the initialization statement of either an OpenMP for loop or
34281 a Cilk Plus for loop.
34282
34283 Return true if the resulting construct should have an
34284 OMP_CLAUSE_PRIVATE added to it. */
34285
34286static tree
34287cp_parser_omp_for_loop_init (cp_parser *parser,
34288 enum tree_code code,
34289 tree &this_pre_body,
34290 vec<tree, va_gc> *&for_block,
34291 tree &init,
34292 tree &orig_init,
34293 tree &decl,
34294 tree &real_decl)
34295{
34296 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34297 return NULL_TREE;
34298
34299 tree add_private_clause = NULL_TREE;
34300
34301 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34302
34303 init-expr:
34304 var = lb
34305 integer-type var = lb
34306 random-access-iterator-type var = lb
34307 pointer-type var = lb
34308 */
34309 cp_decl_specifier_seq type_specifiers;
34310
34311 /* First, try to parse as an initialized declaration. See
34312 cp_parser_condition, from whence the bulk of this is copied. */
34313
34314 cp_parser_parse_tentatively (parser);
34315 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34316 /*is_trailing_return=*/false,
34317 &type_specifiers);
34318 if (cp_parser_parse_definitely (parser))
34319 {
34320 /* If parsing a type specifier seq succeeded, then this
34321 MUST be a initialized declaration. */
34322 tree asm_specification, attributes;
34323 cp_declarator *declarator;
34324
34325 declarator = cp_parser_declarator (parser,
34326 CP_PARSER_DECLARATOR_NAMED,
34327 /*ctor_dtor_or_conv_p=*/NULL,
34328 /*parenthesized_p=*/NULL,
34329 /*member_p=*/false,
34330 /*friend_p=*/false);
34331 attributes = cp_parser_attributes_opt (parser);
34332 asm_specification = cp_parser_asm_specification_opt (parser);
34333
34334 if (declarator == cp_error_declarator)
34335 cp_parser_skip_to_end_of_statement (parser);
34336
34337 else
34338 {
34339 tree pushed_scope, auto_node;
34340
34341 decl = start_decl (declarator, &type_specifiers,
34342 SD_INITIALIZED, attributes,
34343 /*prefix_attributes=*/NULL_TREE,
34344 &pushed_scope);
34345
34346 auto_node = type_uses_auto (TREE_TYPE (decl));
34347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34348 {
34349 if (cp_lexer_next_token_is (parser->lexer,
34350 CPP_OPEN_PAREN))
34351 {
34352 if (code != CILK_SIMD && code != CILK_FOR)
34353 error ("parenthesized initialization is not allowed in "
34354 "OpenMP %<for%> loop");
34355 else
34356 error ("parenthesized initialization is "
34357 "not allowed in for-loop");
34358 }
34359 else
34360 /* Trigger an error. */
34361 cp_parser_require (parser, CPP_EQ, RT_EQ);
34362
34363 init = error_mark_node;
34364 cp_parser_skip_to_end_of_statement (parser);
34365 }
34366 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34367 || type_dependent_expression_p (decl)
34368 || auto_node)
34369 {
34370 bool is_direct_init, is_non_constant_init;
34371
34372 init = cp_parser_initializer (parser,
34373 &is_direct_init,
34374 &is_non_constant_init);
34375
34376 if (auto_node)
34377 {
34378 TREE_TYPE (decl)
34379 = do_auto_deduction (TREE_TYPE (decl), init,
34380 auto_node);
34381
34382 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34383 && !type_dependent_expression_p (decl))
34384 goto non_class;
34385 }
34386
34387 cp_finish_decl (decl, init, !is_non_constant_init,
34388 asm_specification,
34389 LOOKUP_ONLYCONVERTING);
34390 orig_init = init;
34391 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34392 {
34393 vec_safe_push (for_block, this_pre_body);
34394 init = NULL_TREE;
34395 }
34396 else
34397 {
34398 init = pop_stmt_list (this_pre_body);
34399 if (init && TREE_CODE (init) == STATEMENT_LIST)
34400 {
34401 tree_stmt_iterator i = tsi_start (init);
34402 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34403 while (!tsi_end_p (i))
34404 {
34405 tree t = tsi_stmt (i);
34406 if (TREE_CODE (t) == DECL_EXPR
34407 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34408 {
34409 tsi_delink (&i);
34410 vec_safe_push (for_block, t);
34411 continue;
34412 }
34413 break;
34414 }
34415 if (tsi_one_before_end_p (i))
34416 {
34417 tree t = tsi_stmt (i);
34418 tsi_delink (&i);
34419 free_stmt_list (init);
34420 init = t;
34421 }
34422 }
34423 }
34424 this_pre_body = NULL_TREE;
34425 }
34426 else
34427 {
34428 /* Consume '='. */
34429 cp_lexer_consume_token (parser->lexer);
34430 init = cp_parser_assignment_expression (parser);
34431
34432 non_class:
34433 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34434 init = error_mark_node;
34435 else
34436 cp_finish_decl (decl, NULL_TREE,
34437 /*init_const_expr_p=*/false,
34438 asm_specification,
34439 LOOKUP_ONLYCONVERTING);
34440 }
34441
34442 if (pushed_scope)
34443 pop_scope (pushed_scope);
34444 }
34445 }
34446 else
34447 {
34448 cp_id_kind idk;
34449 /* If parsing a type specifier sequence failed, then
34450 this MUST be a simple expression. */
34451 if (code == CILK_FOR)
34452 error ("%<_Cilk_for%> allows expression instead of declaration only "
34453 "in C, not in C++");
34454 cp_parser_parse_tentatively (parser);
34455 decl = cp_parser_primary_expression (parser, false, false,
34456 false, &idk);
34457 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34458 if (!cp_parser_error_occurred (parser)
34459 && decl
34460 && (TREE_CODE (decl) == COMPONENT_REF
34461 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34462 {
34463 cp_parser_abort_tentative_parse (parser);
34464 cp_parser_parse_tentatively (parser);
34465 cp_token *token = cp_lexer_peek_token (parser->lexer);
34466 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34467 /*check_dependency_p=*/true,
34468 /*template_p=*/NULL,
34469 /*declarator_p=*/false,
34470 /*optional_p=*/false);
34471 if (name != error_mark_node
34472 && last_tok == cp_lexer_peek_token (parser->lexer))
34473 {
34474 decl = cp_parser_lookup_name_simple (parser, name,
34475 token->location);
34476 if (TREE_CODE (decl) == FIELD_DECL)
34477 add_private_clause = omp_privatize_field (decl, false);
34478 }
34479 cp_parser_abort_tentative_parse (parser);
34480 cp_parser_parse_tentatively (parser);
34481 decl = cp_parser_primary_expression (parser, false, false,
34482 false, &idk);
34483 }
34484 if (!cp_parser_error_occurred (parser)
34485 && decl
34486 && DECL_P (decl)
34487 && CLASS_TYPE_P (TREE_TYPE (decl)))
34488 {
34489 tree rhs;
34490
34491 cp_parser_parse_definitely (parser);
34492 cp_parser_require (parser, CPP_EQ, RT_EQ);
34493 rhs = cp_parser_assignment_expression (parser);
34494 orig_init = rhs;
34495 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34496 decl, NOP_EXPR,
34497 rhs,
34498 tf_warning_or_error));
34499 if (!add_private_clause)
34500 add_private_clause = decl;
34501 }
34502 else
34503 {
34504 decl = NULL;
34505 cp_parser_abort_tentative_parse (parser);
34506 init = cp_parser_expression (parser);
34507 if (init)
34508 {
34509 if (TREE_CODE (init) == MODIFY_EXPR
34510 || TREE_CODE (init) == MODOP_EXPR)
34511 real_decl = TREE_OPERAND (init, 0);
34512 }
34513 }
34514 }
34515 return add_private_clause;
34516}
34517
34518/* Parse the restricted form of the for statement allowed by OpenMP. */
34519
34520static tree
34521cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34522 tree *cclauses, bool *if_p)
34523{
34524 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34525 tree real_decl, initv, condv, incrv, declv;
34526 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34527 location_t loc_first;
34528 bool collapse_err = false;
34529 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34530 vec<tree, va_gc> *for_block = make_tree_vector ();
34531 auto_vec<tree, 4> orig_inits;
34532 bool tiling = false;
34533
34534 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34535 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34536 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34537 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34538 {
34539 tiling = true;
34540 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34541 }
34542 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34543 && OMP_CLAUSE_ORDERED_EXPR (cl))
34544 {
34545 ordered_cl = cl;
34546 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34547 }
34548
34549 if (ordered && ordered < collapse)
34550 {
34551 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34552 "%<ordered%> clause parameter is less than %<collapse%>");
34553 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34554 = build_int_cst (NULL_TREE, collapse);
34555 ordered = collapse;
34556 }
34557 if (ordered)
34558 {
34559 for (tree *pc = &clauses; *pc; )
34560 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34561 {
34562 error_at (OMP_CLAUSE_LOCATION (*pc),
34563 "%<linear%> clause may not be specified together "
34564 "with %<ordered%> clause with a parameter");
34565 *pc = OMP_CLAUSE_CHAIN (*pc);
34566 }
34567 else
34568 pc = &OMP_CLAUSE_CHAIN (*pc);
34569 }
34570
34571 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34572 count = ordered ? ordered : collapse;
34573
34574 declv = make_tree_vec (count);
34575 initv = make_tree_vec (count);
34576 condv = make_tree_vec (count);
34577 incrv = make_tree_vec (count);
34578
34579 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34580
34581 for (i = 0; i < count; i++)
34582 {
34583 int bracecount = 0;
34584 tree add_private_clause = NULL_TREE;
34585 location_t loc;
34586
34587 if (code != CILK_FOR
34588 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34589 {
34590 if (!collapse_err)
34591 cp_parser_error (parser, "for statement expected");
34592 return NULL;
34593 }
34594 if (code == CILK_FOR
34595 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34596 {
34597 if (!collapse_err)
34598 cp_parser_error (parser, "_Cilk_for statement expected");
34599 return NULL;
34600 }
34601 loc = cp_lexer_consume_token (parser->lexer)->location;
34602
34603 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34604 return NULL;
34605
34606 init = orig_init = decl = real_decl = NULL;
34607 this_pre_body = push_stmt_list ();
34608
34609 add_private_clause
34610 = cp_parser_omp_for_loop_init (parser, code,
34611 this_pre_body, for_block,
34612 init, orig_init, decl, real_decl);
34613
34614 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34615 if (this_pre_body)
34616 {
34617 this_pre_body = pop_stmt_list (this_pre_body);
34618 if (pre_body)
34619 {
34620 tree t = pre_body;
34621 pre_body = push_stmt_list ();
34622 add_stmt (t);
34623 add_stmt (this_pre_body);
34624 pre_body = pop_stmt_list (pre_body);
34625 }
34626 else
34627 pre_body = this_pre_body;
34628 }
34629
34630 if (decl)
34631 real_decl = decl;
34632 if (cclauses != NULL
34633 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34634 && real_decl != NULL_TREE)
34635 {
34636 tree *c;
34637 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34638 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34639 && OMP_CLAUSE_DECL (*c) == real_decl)
34640 {
34641 error_at (loc, "iteration variable %qD"
34642 " should not be firstprivate", real_decl);
34643 *c = OMP_CLAUSE_CHAIN (*c);
34644 }
34645 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34646 && OMP_CLAUSE_DECL (*c) == real_decl)
34647 {
34648 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34649 tree l = *c;
34650 *c = OMP_CLAUSE_CHAIN (*c);
34651 if (code == OMP_SIMD)
34652 {
34653 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34654 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34655 }
34656 else
34657 {
34658 OMP_CLAUSE_CHAIN (l) = clauses;
34659 clauses = l;
34660 }
34661 add_private_clause = NULL_TREE;
34662 }
34663 else
34664 {
34665 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34666 && OMP_CLAUSE_DECL (*c) == real_decl)
34667 add_private_clause = NULL_TREE;
34668 c = &OMP_CLAUSE_CHAIN (*c);
34669 }
34670 }
34671
34672 if (add_private_clause)
34673 {
34674 tree c;
34675 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34676 {
34677 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34678 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34679 && OMP_CLAUSE_DECL (c) == decl)
34680 break;
34681 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34682 && OMP_CLAUSE_DECL (c) == decl)
34683 error_at (loc, "iteration variable %qD "
34684 "should not be firstprivate",
34685 decl);
34686 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34687 && OMP_CLAUSE_DECL (c) == decl)
34688 error_at (loc, "iteration variable %qD should not be reduction",
34689 decl);
34690 }
34691 if (c == NULL)
34692 {
34693 if (code != OMP_SIMD)
34694 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34695 else if (collapse == 1)
34696 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34697 else
34698 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34699 OMP_CLAUSE_DECL (c) = add_private_clause;
34700 c = finish_omp_clauses (c, C_ORT_OMP);
34701 if (c)
34702 {
34703 OMP_CLAUSE_CHAIN (c) = clauses;
34704 clauses = c;
34705 /* For linear, signal that we need to fill up
34706 the so far unknown linear step. */
34707 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34708 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34709 }
34710 }
34711 }
34712
34713 cond = NULL;
34714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34715 cond = cp_parser_omp_for_cond (parser, decl, code);
34716 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34717
34718 incr = NULL;
34719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34720 {
34721 /* If decl is an iterator, preserve the operator on decl
34722 until finish_omp_for. */
34723 if (real_decl
34724 && ((processing_template_decl
34725 && (TREE_TYPE (real_decl) == NULL_TREE
34726 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34727 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34728 incr = cp_parser_omp_for_incr (parser, real_decl);
34729 else
34730 incr = cp_parser_expression (parser);
34731 if (!EXPR_HAS_LOCATION (incr))
34732 protected_set_expr_location (incr, input_location);
34733 }
34734
34735 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34736 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34737 /*or_comma=*/false,
34738 /*consume_paren=*/true);
34739
34740 TREE_VEC_ELT (declv, i) = decl;
34741 TREE_VEC_ELT (initv, i) = init;
34742 TREE_VEC_ELT (condv, i) = cond;
34743 TREE_VEC_ELT (incrv, i) = incr;
34744 if (orig_init)
34745 {
34746 orig_inits.safe_grow_cleared (i + 1);
34747 orig_inits[i] = orig_init;
34748 }
34749
34750 if (i == count - 1)
34751 break;
34752
34753 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34754 in between the collapsed for loops to be still considered perfectly
34755 nested. Hopefully the final version clarifies this.
34756 For now handle (multiple) {'s and empty statements. */
34757 cp_parser_parse_tentatively (parser);
34758 for (;;)
34759 {
34760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34761 break;
34762 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34763 {
34764 cp_lexer_consume_token (parser->lexer);
34765 bracecount++;
34766 }
34767 else if (bracecount
34768 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34769 cp_lexer_consume_token (parser->lexer);
34770 else
34771 {
34772 loc = cp_lexer_peek_token (parser->lexer)->location;
34773 error_at (loc, "not enough for loops to collapse");
34774 collapse_err = true;
34775 cp_parser_abort_tentative_parse (parser);
34776 declv = NULL_TREE;
34777 break;
34778 }
34779 }
34780
34781 if (declv)
34782 {
34783 cp_parser_parse_definitely (parser);
34784 nbraces += bracecount;
34785 }
34786 }
34787
34788 if (nbraces)
34789 if_p = NULL;
34790
34791 /* Note that we saved the original contents of this flag when we entered
34792 the structured block, and so we don't need to re-save it here. */
34793 if (code == CILK_SIMD || code == CILK_FOR)
34794 parser->in_statement = IN_CILK_SIMD_FOR;
34795 else
34796 parser->in_statement = IN_OMP_FOR;
34797
34798 /* Note that the grammar doesn't call for a structured block here,
34799 though the loop as a whole is a structured block. */
34800 body = push_stmt_list ();
34801 cp_parser_statement (parser, NULL_TREE, false, if_p);
34802 body = pop_stmt_list (body);
34803
34804 if (declv == NULL_TREE)
34805 ret = NULL_TREE;
34806 else
34807 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34808 body, pre_body, &orig_inits, clauses);
34809
34810 while (nbraces)
34811 {
34812 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34813 {
34814 cp_lexer_consume_token (parser->lexer);
34815 nbraces--;
34816 }
34817 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34818 cp_lexer_consume_token (parser->lexer);
34819 else
34820 {
34821 if (!collapse_err)
34822 {
34823 error_at (cp_lexer_peek_token (parser->lexer)->location,
34824 "collapsed loops not perfectly nested");
34825 }
34826 collapse_err = true;
34827 cp_parser_statement_seq_opt (parser, NULL);
34828 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34829 break;
34830 }
34831 }
34832
34833 while (!for_block->is_empty ())
34834 {
34835 tree t = for_block->pop ();
34836 if (TREE_CODE (t) == STATEMENT_LIST)
34837 add_stmt (pop_stmt_list (t));
34838 else
34839 add_stmt (t);
34840 }
34841 release_tree_vector (for_block);
34842
34843 return ret;
34844}
34845
34846/* Helper function for OpenMP parsing, split clauses and call
34847 finish_omp_clauses on each of the set of clauses afterwards. */
34848
34849static void
34850cp_omp_split_clauses (location_t loc, enum tree_code code,
34851 omp_clause_mask mask, tree clauses, tree *cclauses)
34852{
34853 int i;
34854 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34855 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34856 if (cclauses[i])
34857 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34858}
34859
34860/* OpenMP 4.0:
34861 #pragma omp simd simd-clause[optseq] new-line
34862 for-loop */
34863
34864#define OMP_SIMD_CLAUSE_MASK \
34865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34873
34874static tree
34875cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34876 char *p_name, omp_clause_mask mask, tree *cclauses,
34877 bool *if_p)
34878{
34879 tree clauses, sb, ret;
34880 unsigned int save;
34881 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34882
34883 strcat (p_name, " simd");
34884 mask |= OMP_SIMD_CLAUSE_MASK;
34885
34886 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34887 cclauses == NULL);
34888 if (cclauses)
34889 {
34890 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34891 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34892 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34893 OMP_CLAUSE_ORDERED);
34894 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34895 {
34896 error_at (OMP_CLAUSE_LOCATION (c),
34897 "%<ordered%> clause with parameter may not be specified "
34898 "on %qs construct", p_name);
34899 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34900 }
34901 }
34902
34903 sb = begin_omp_structured_block ();
34904 save = cp_parser_begin_omp_structured_block (parser);
34905
34906 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34907
34908 cp_parser_end_omp_structured_block (parser, save);
34909 add_stmt (finish_omp_structured_block (sb));
34910
34911 return ret;
34912}
34913
34914/* OpenMP 2.5:
34915 #pragma omp for for-clause[optseq] new-line
34916 for-loop
34917
34918 OpenMP 4.0:
34919 #pragma omp for simd for-simd-clause[optseq] new-line
34920 for-loop */
34921
34922#define OMP_FOR_CLAUSE_MASK \
34923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34932
34933static tree
34934cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34935 char *p_name, omp_clause_mask mask, tree *cclauses,
34936 bool *if_p)
34937{
34938 tree clauses, sb, ret;
34939 unsigned int save;
34940 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34941
34942 strcat (p_name, " for");
34943 mask |= OMP_FOR_CLAUSE_MASK;
34944 /* parallel for{, simd} disallows nowait clause, but for
34945 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34946 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34947 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34948 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34949 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34950 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34951
34952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34953 {
34954 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34955 const char *p = IDENTIFIER_POINTER (id);
34956
34957 if (strcmp (p, "simd") == 0)
34958 {
34959 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34960 if (cclauses == NULL)
34961 cclauses = cclauses_buf;
34962
34963 cp_lexer_consume_token (parser->lexer);
34964 if (!flag_openmp) /* flag_openmp_simd */
34965 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34966 cclauses, if_p);
34967 sb = begin_omp_structured_block ();
34968 save = cp_parser_begin_omp_structured_block (parser);
34969 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34970 cclauses, if_p);
34971 cp_parser_end_omp_structured_block (parser, save);
34972 tree body = finish_omp_structured_block (sb);
34973 if (ret == NULL)
34974 return ret;
34975 ret = make_node (OMP_FOR);
34976 TREE_TYPE (ret) = void_type_node;
34977 OMP_FOR_BODY (ret) = body;
34978 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34979 SET_EXPR_LOCATION (ret, loc);
34980 add_stmt (ret);
34981 return ret;
34982 }
34983 }
34984 if (!flag_openmp) /* flag_openmp_simd */
34985 {
34986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34987 return NULL_TREE;
34988 }
34989
34990 /* Composite distribute parallel for disallows linear clause. */
34991 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34992 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34993
34994 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34995 cclauses == NULL);
34996 if (cclauses)
34997 {
34998 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34999 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35000 }
35001
35002 sb = begin_omp_structured_block ();
35003 save = cp_parser_begin_omp_structured_block (parser);
35004
35005 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35006
35007 cp_parser_end_omp_structured_block (parser, save);
35008 add_stmt (finish_omp_structured_block (sb));
35009
35010 return ret;
35011}
35012
35013/* OpenMP 2.5:
35014 # pragma omp master new-line
35015 structured-block */
35016
35017static tree
35018cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35019{
35020 cp_parser_require_pragma_eol (parser, pragma_tok);
35021 return c_finish_omp_master (input_location,
35022 cp_parser_omp_structured_block (parser, if_p));
35023}
35024
35025/* OpenMP 2.5:
35026 # pragma omp ordered new-line
35027 structured-block
35028
35029 OpenMP 4.5:
35030 # pragma omp ordered ordered-clauses new-line
35031 structured-block */
35032
35033#define OMP_ORDERED_CLAUSE_MASK \
35034 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35036
35037#define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35038 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35039
35040static bool
35041cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35042 enum pragma_context context, bool *if_p)
35043{
35044 location_t loc = pragma_tok->location;
35045
35046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35047 {
35048 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35049 const char *p = IDENTIFIER_POINTER (id);
35050
35051 if (strcmp (p, "depend") == 0)
35052 {
35053 if (context == pragma_stmt)
35054 {
35055 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35056 "%<depend%> clause may only be used in compound "
35057 "statements");
35058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35059 return false;
35060 }
35061 tree clauses
35062 = cp_parser_omp_all_clauses (parser,
35063 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35064 "#pragma omp ordered", pragma_tok);
35065 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35066 return false;
35067 }
35068 }
35069
35070 tree clauses
35071 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35072 "#pragma omp ordered", pragma_tok);
35073 c_finish_omp_ordered (loc, clauses,
35074 cp_parser_omp_structured_block (parser, if_p));
35075 return true;
35076}
35077
35078/* OpenMP 2.5:
35079
35080 section-scope:
35081 { section-sequence }
35082
35083 section-sequence:
35084 section-directive[opt] structured-block
35085 section-sequence section-directive structured-block */
35086
35087static tree
35088cp_parser_omp_sections_scope (cp_parser *parser)
35089{
35090 tree stmt, substmt;
35091 bool error_suppress = false;
35092 cp_token *tok;
35093
35094 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35095 return NULL_TREE;
35096
35097 stmt = push_stmt_list ();
35098
35099 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35100 != PRAGMA_OMP_SECTION)
35101 {
35102 substmt = cp_parser_omp_structured_block (parser, NULL);
35103 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35104 add_stmt (substmt);
35105 }
35106
35107 while (1)
35108 {
35109 tok = cp_lexer_peek_token (parser->lexer);
35110 if (tok->type == CPP_CLOSE_BRACE)
35111 break;
35112 if (tok->type == CPP_EOF)
35113 break;
35114
35115 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35116 {
35117 cp_lexer_consume_token (parser->lexer);
35118 cp_parser_require_pragma_eol (parser, tok);
35119 error_suppress = false;
35120 }
35121 else if (!error_suppress)
35122 {
35123 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35124 error_suppress = true;
35125 }
35126
35127 substmt = cp_parser_omp_structured_block (parser, NULL);
35128 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35129 add_stmt (substmt);
35130 }
35131 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35132
35133 substmt = pop_stmt_list (stmt);
35134
35135 stmt = make_node (OMP_SECTIONS);
35136 TREE_TYPE (stmt) = void_type_node;
35137 OMP_SECTIONS_BODY (stmt) = substmt;
35138
35139 add_stmt (stmt);
35140 return stmt;
35141}
35142
35143/* OpenMP 2.5:
35144 # pragma omp sections sections-clause[optseq] newline
35145 sections-scope */
35146
35147#define OMP_SECTIONS_CLAUSE_MASK \
35148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35153
35154static tree
35155cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35156 char *p_name, omp_clause_mask mask, tree *cclauses)
35157{
35158 tree clauses, ret;
35159 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35160
35161 strcat (p_name, " sections");
35162 mask |= OMP_SECTIONS_CLAUSE_MASK;
35163 if (cclauses)
35164 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35165
35166 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35167 cclauses == NULL);
35168 if (cclauses)
35169 {
35170 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35171 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35172 }
35173
35174 ret = cp_parser_omp_sections_scope (parser);
35175 if (ret)
35176 OMP_SECTIONS_CLAUSES (ret) = clauses;
35177
35178 return ret;
35179}
35180
35181/* OpenMP 2.5:
35182 # pragma omp parallel parallel-clause[optseq] new-line
35183 structured-block
35184 # pragma omp parallel for parallel-for-clause[optseq] new-line
35185 structured-block
35186 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35187 structured-block
35188
35189 OpenMP 4.0:
35190 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35191 structured-block */
35192
35193#define OMP_PARALLEL_CLAUSE_MASK \
35194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35203
35204static tree
35205cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35206 char *p_name, omp_clause_mask mask, tree *cclauses,
35207 bool *if_p)
35208{
35209 tree stmt, clauses, block;
35210 unsigned int save;
35211 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35212
35213 strcat (p_name, " parallel");
35214 mask |= OMP_PARALLEL_CLAUSE_MASK;
35215 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35216 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35217 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35218 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35219
35220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35221 {
35222 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35223 if (cclauses == NULL)
35224 cclauses = cclauses_buf;
35225
35226 cp_lexer_consume_token (parser->lexer);
35227 if (!flag_openmp) /* flag_openmp_simd */
35228 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35229 if_p);
35230 block = begin_omp_parallel ();
35231 save = cp_parser_begin_omp_structured_block (parser);
35232 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35233 if_p);
35234 cp_parser_end_omp_structured_block (parser, save);
35235 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35236 block);
35237 if (ret == NULL_TREE)
35238 return ret;
35239 OMP_PARALLEL_COMBINED (stmt) = 1;
35240 return stmt;
35241 }
35242 /* When combined with distribute, parallel has to be followed by for.
35243 #pragma omp target parallel is allowed though. */
35244 else if (cclauses
35245 && (mask & (OMP_CLAUSE_MASK_1
35246 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35247 {
35248 error_at (loc, "expected %<for%> after %qs", p_name);
35249 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35250 return NULL_TREE;
35251 }
35252 else if (!flag_openmp) /* flag_openmp_simd */
35253 {
35254 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35255 return NULL_TREE;
35256 }
35257 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35258 {
35259 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35260 const char *p = IDENTIFIER_POINTER (id);
35261 if (strcmp (p, "sections") == 0)
35262 {
35263 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35264 cclauses = cclauses_buf;
35265
35266 cp_lexer_consume_token (parser->lexer);
35267 block = begin_omp_parallel ();
35268 save = cp_parser_begin_omp_structured_block (parser);
35269 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35270 cp_parser_end_omp_structured_block (parser, save);
35271 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35272 block);
35273 OMP_PARALLEL_COMBINED (stmt) = 1;
35274 return stmt;
35275 }
35276 }
35277
35278 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35279 cclauses == NULL);
35280 if (cclauses)
35281 {
35282 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35283 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35284 }
35285
35286 block = begin_omp_parallel ();
35287 save = cp_parser_begin_omp_structured_block (parser);
35288 cp_parser_statement (parser, NULL_TREE, false, if_p);
35289 cp_parser_end_omp_structured_block (parser, save);
35290 stmt = finish_omp_parallel (clauses, block);
35291 return stmt;
35292}
35293
35294/* OpenMP 2.5:
35295 # pragma omp single single-clause[optseq] new-line
35296 structured-block */
35297
35298#define OMP_SINGLE_CLAUSE_MASK \
35299 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35303
35304static tree
35305cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35306{
35307 tree stmt = make_node (OMP_SINGLE);
35308 TREE_TYPE (stmt) = void_type_node;
35309
35310 OMP_SINGLE_CLAUSES (stmt)
35311 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35312 "#pragma omp single", pragma_tok);
35313 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35314
35315 return add_stmt (stmt);
35316}
35317
35318/* OpenMP 3.0:
35319 # pragma omp task task-clause[optseq] new-line
35320 structured-block */
35321
35322#define OMP_TASK_CLAUSE_MASK \
35323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35333
35334static tree
35335cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35336{
35337 tree clauses, block;
35338 unsigned int save;
35339
35340 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35341 "#pragma omp task", pragma_tok);
35342 block = begin_omp_task ();
35343 save = cp_parser_begin_omp_structured_block (parser);
35344 cp_parser_statement (parser, NULL_TREE, false, if_p);
35345 cp_parser_end_omp_structured_block (parser, save);
35346 return finish_omp_task (clauses, block);
35347}
35348
35349/* OpenMP 3.0:
35350 # pragma omp taskwait new-line */
35351
35352static void
35353cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35354{
35355 cp_parser_require_pragma_eol (parser, pragma_tok);
35356 finish_omp_taskwait ();
35357}
35358
35359/* OpenMP 3.1:
35360 # pragma omp taskyield new-line */
35361
35362static void
35363cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35364{
35365 cp_parser_require_pragma_eol (parser, pragma_tok);
35366 finish_omp_taskyield ();
35367}
35368
35369/* OpenMP 4.0:
35370 # pragma omp taskgroup new-line
35371 structured-block */
35372
35373static tree
35374cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35375{
35376 cp_parser_require_pragma_eol (parser, pragma_tok);
35377 return c_finish_omp_taskgroup (input_location,
35378 cp_parser_omp_structured_block (parser,
35379 if_p));
35380}
35381
35382
35383/* OpenMP 2.5:
35384 # pragma omp threadprivate (variable-list) */
35385
35386static void
35387cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35388{
35389 tree vars;
35390
35391 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35392 cp_parser_require_pragma_eol (parser, pragma_tok);
35393
35394 finish_omp_threadprivate (vars);
35395}
35396
35397/* OpenMP 4.0:
35398 # pragma omp cancel cancel-clause[optseq] new-line */
35399
35400#define OMP_CANCEL_CLAUSE_MASK \
35401 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35406
35407static void
35408cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35409{
35410 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35411 "#pragma omp cancel", pragma_tok);
35412 finish_omp_cancel (clauses);
35413}
35414
35415/* OpenMP 4.0:
35416 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35417
35418#define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35423
35424static void
35425cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35426 enum pragma_context context)
35427{
35428 tree clauses;
35429 bool point_seen = false;
35430
35431 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35432 {
35433 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35434 const char *p = IDENTIFIER_POINTER (id);
35435
35436 if (strcmp (p, "point") == 0)
35437 {
35438 cp_lexer_consume_token (parser->lexer);
35439 point_seen = true;
35440 }
35441 }
35442 if (!point_seen)
35443 {
35444 cp_parser_error (parser, "expected %<point%>");
35445 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35446 return;
35447 }
35448
35449 if (context != pragma_compound)
35450 {
35451 if (context == pragma_stmt)
35452 error_at (pragma_tok->location,
35453 "%<#pragma %s%> may only be used in compound statements",
35454 "omp cancellation point");
35455 else
35456 cp_parser_error (parser, "expected declaration specifiers");
35457 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35458 return;
35459 }
35460
35461 clauses = cp_parser_omp_all_clauses (parser,
35462 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35463 "#pragma omp cancellation point",
35464 pragma_tok);
35465 finish_omp_cancellation_point (clauses);
35466}
35467
35468/* OpenMP 4.0:
35469 #pragma omp distribute distribute-clause[optseq] new-line
35470 for-loop */
35471
35472#define OMP_DISTRIBUTE_CLAUSE_MASK \
35473 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35478
35479static tree
35480cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35481 char *p_name, omp_clause_mask mask, tree *cclauses,
35482 bool *if_p)
35483{
35484 tree clauses, sb, ret;
35485 unsigned int save;
35486 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35487
35488 strcat (p_name, " distribute");
35489 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35490
35491 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35492 {
35493 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35494 const char *p = IDENTIFIER_POINTER (id);
35495 bool simd = false;
35496 bool parallel = false;
35497
35498 if (strcmp (p, "simd") == 0)
35499 simd = true;
35500 else
35501 parallel = strcmp (p, "parallel") == 0;
35502 if (parallel || simd)
35503 {
35504 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35505 if (cclauses == NULL)
35506 cclauses = cclauses_buf;
35507 cp_lexer_consume_token (parser->lexer);
35508 if (!flag_openmp) /* flag_openmp_simd */
35509 {
35510 if (simd)
35511 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35512 cclauses, if_p);
35513 else
35514 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35515 cclauses, if_p);
35516 }
35517 sb = begin_omp_structured_block ();
35518 save = cp_parser_begin_omp_structured_block (parser);
35519 if (simd)
35520 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35521 cclauses, if_p);
35522 else
35523 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35524 cclauses, if_p);
35525 cp_parser_end_omp_structured_block (parser, save);
35526 tree body = finish_omp_structured_block (sb);
35527 if (ret == NULL)
35528 return ret;
35529 ret = make_node (OMP_DISTRIBUTE);
35530 TREE_TYPE (ret) = void_type_node;
35531 OMP_FOR_BODY (ret) = body;
35532 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35533 SET_EXPR_LOCATION (ret, loc);
35534 add_stmt (ret);
35535 return ret;
35536 }
35537 }
35538 if (!flag_openmp) /* flag_openmp_simd */
35539 {
35540 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35541 return NULL_TREE;
35542 }
35543
35544 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35545 cclauses == NULL);
35546 if (cclauses)
35547 {
35548 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35549 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35550 }
35551
35552 sb = begin_omp_structured_block ();
35553 save = cp_parser_begin_omp_structured_block (parser);
35554
35555 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35556
35557 cp_parser_end_omp_structured_block (parser, save);
35558 add_stmt (finish_omp_structured_block (sb));
35559
35560 return ret;
35561}
35562
35563/* OpenMP 4.0:
35564 # pragma omp teams teams-clause[optseq] new-line
35565 structured-block */
35566
35567#define OMP_TEAMS_CLAUSE_MASK \
35568 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35575
35576static tree
35577cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35578 char *p_name, omp_clause_mask mask, tree *cclauses,
35579 bool *if_p)
35580{
35581 tree clauses, sb, ret;
35582 unsigned int save;
35583 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35584
35585 strcat (p_name, " teams");
35586 mask |= OMP_TEAMS_CLAUSE_MASK;
35587
35588 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35589 {
35590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35591 const char *p = IDENTIFIER_POINTER (id);
35592 if (strcmp (p, "distribute") == 0)
35593 {
35594 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35595 if (cclauses == NULL)
35596 cclauses = cclauses_buf;
35597
35598 cp_lexer_consume_token (parser->lexer);
35599 if (!flag_openmp) /* flag_openmp_simd */
35600 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35601 cclauses, if_p);
35602 sb = begin_omp_structured_block ();
35603 save = cp_parser_begin_omp_structured_block (parser);
35604 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35605 cclauses, if_p);
35606 cp_parser_end_omp_structured_block (parser, save);
35607 tree body = finish_omp_structured_block (sb);
35608 if (ret == NULL)
35609 return ret;
35610 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35611 ret = make_node (OMP_TEAMS);
35612 TREE_TYPE (ret) = void_type_node;
35613 OMP_TEAMS_CLAUSES (ret) = clauses;
35614 OMP_TEAMS_BODY (ret) = body;
35615 OMP_TEAMS_COMBINED (ret) = 1;
35616 SET_EXPR_LOCATION (ret, loc);
35617 return add_stmt (ret);
35618 }
35619 }
35620 if (!flag_openmp) /* flag_openmp_simd */
35621 {
35622 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35623 return NULL_TREE;
35624 }
35625
35626 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35627 cclauses == NULL);
35628 if (cclauses)
35629 {
35630 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35631 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35632 }
35633
35634 tree stmt = make_node (OMP_TEAMS);
35635 TREE_TYPE (stmt) = void_type_node;
35636 OMP_TEAMS_CLAUSES (stmt) = clauses;
35637 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35638 SET_EXPR_LOCATION (stmt, loc);
35639
35640 return add_stmt (stmt);
35641}
35642
35643/* OpenMP 4.0:
35644 # pragma omp target data target-data-clause[optseq] new-line
35645 structured-block */
35646
35647#define OMP_TARGET_DATA_CLAUSE_MASK \
35648 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35652
35653static tree
35654cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35655{
35656 tree clauses
35657 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35658 "#pragma omp target data", pragma_tok);
35659 int map_seen = 0;
35660 for (tree *pc = &clauses; *pc;)
35661 {
35662 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35663 switch (OMP_CLAUSE_MAP_KIND (*pc))
35664 {
35665 case GOMP_MAP_TO:
35666 case GOMP_MAP_ALWAYS_TO:
35667 case GOMP_MAP_FROM:
35668 case GOMP_MAP_ALWAYS_FROM:
35669 case GOMP_MAP_TOFROM:
35670 case GOMP_MAP_ALWAYS_TOFROM:
35671 case GOMP_MAP_ALLOC:
35672 map_seen = 3;
35673 break;
35674 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35675 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35676 case GOMP_MAP_ALWAYS_POINTER:
35677 break;
35678 default:
35679 map_seen |= 1;
35680 error_at (OMP_CLAUSE_LOCATION (*pc),
35681 "%<#pragma omp target data%> with map-type other "
35682 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35683 "on %<map%> clause");
35684 *pc = OMP_CLAUSE_CHAIN (*pc);
35685 continue;
35686 }
35687 pc = &OMP_CLAUSE_CHAIN (*pc);
35688 }
35689
35690 if (map_seen != 3)
35691 {
35692 if (map_seen == 0)
35693 error_at (pragma_tok->location,
35694 "%<#pragma omp target data%> must contain at least "
35695 "one %<map%> clause");
35696 return NULL_TREE;
35697 }
35698
35699 tree stmt = make_node (OMP_TARGET_DATA);
35700 TREE_TYPE (stmt) = void_type_node;
35701 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35702
35703 keep_next_level (true);
35704 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35705
35706 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35707 return add_stmt (stmt);
35708}
35709
35710/* OpenMP 4.5:
35711 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35712 structured-block */
35713
35714#define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35720
35721static tree
35722cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35723 enum pragma_context context)
35724{
35725 bool data_seen = false;
35726 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35727 {
35728 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35729 const char *p = IDENTIFIER_POINTER (id);
35730
35731 if (strcmp (p, "data") == 0)
35732 {
35733 cp_lexer_consume_token (parser->lexer);
35734 data_seen = true;
35735 }
35736 }
35737 if (!data_seen)
35738 {
35739 cp_parser_error (parser, "expected %<data%>");
35740 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35741 return NULL_TREE;
35742 }
35743
35744 if (context == pragma_stmt)
35745 {
35746 error_at (pragma_tok->location,
35747 "%<#pragma %s%> may only be used in compound statements",
35748 "omp target enter data");
35749 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35750 return NULL_TREE;
35751 }
35752
35753 tree clauses
35754 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35755 "#pragma omp target enter data", pragma_tok);
35756 int map_seen = 0;
35757 for (tree *pc = &clauses; *pc;)
35758 {
35759 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35760 switch (OMP_CLAUSE_MAP_KIND (*pc))
35761 {
35762 case GOMP_MAP_TO:
35763 case GOMP_MAP_ALWAYS_TO:
35764 case GOMP_MAP_ALLOC:
35765 map_seen = 3;
35766 break;
35767 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35768 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35769 case GOMP_MAP_ALWAYS_POINTER:
35770 break;
35771 default:
35772 map_seen |= 1;
35773 error_at (OMP_CLAUSE_LOCATION (*pc),
35774 "%<#pragma omp target enter data%> with map-type other "
35775 "than %<to%> or %<alloc%> on %<map%> clause");
35776 *pc = OMP_CLAUSE_CHAIN (*pc);
35777 continue;
35778 }
35779 pc = &OMP_CLAUSE_CHAIN (*pc);
35780 }
35781
35782 if (map_seen != 3)
35783 {
35784 if (map_seen == 0)
35785 error_at (pragma_tok->location,
35786 "%<#pragma omp target enter data%> must contain at least "
35787 "one %<map%> clause");
35788 return NULL_TREE;
35789 }
35790
35791 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35792 TREE_TYPE (stmt) = void_type_node;
35793 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35794 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35795 return add_stmt (stmt);
35796}
35797
35798/* OpenMP 4.5:
35799 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35800 structured-block */
35801
35802#define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35803 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35808
35809static tree
35810cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35811 enum pragma_context context)
35812{
35813 bool data_seen = false;
35814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35815 {
35816 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35817 const char *p = IDENTIFIER_POINTER (id);
35818
35819 if (strcmp (p, "data") == 0)
35820 {
35821 cp_lexer_consume_token (parser->lexer);
35822 data_seen = true;
35823 }
35824 }
35825 if (!data_seen)
35826 {
35827 cp_parser_error (parser, "expected %<data%>");
35828 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35829 return NULL_TREE;
35830 }
35831
35832 if (context == pragma_stmt)
35833 {
35834 error_at (pragma_tok->location,
35835 "%<#pragma %s%> may only be used in compound statements",
35836 "omp target exit data");
35837 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35838 return NULL_TREE;
35839 }
35840
35841 tree clauses
35842 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35843 "#pragma omp target exit data", pragma_tok);
35844 int map_seen = 0;
35845 for (tree *pc = &clauses; *pc;)
35846 {
35847 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35848 switch (OMP_CLAUSE_MAP_KIND (*pc))
35849 {
35850 case GOMP_MAP_FROM:
35851 case GOMP_MAP_ALWAYS_FROM:
35852 case GOMP_MAP_RELEASE:
35853 case GOMP_MAP_DELETE:
35854 map_seen = 3;
35855 break;
35856 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35857 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35858 case GOMP_MAP_ALWAYS_POINTER:
35859 break;
35860 default:
35861 map_seen |= 1;
35862 error_at (OMP_CLAUSE_LOCATION (*pc),
35863 "%<#pragma omp target exit data%> with map-type other "
35864 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35865 " clause");
35866 *pc = OMP_CLAUSE_CHAIN (*pc);
35867 continue;
35868 }
35869 pc = &OMP_CLAUSE_CHAIN (*pc);
35870 }
35871
35872 if (map_seen != 3)
35873 {
35874 if (map_seen == 0)
35875 error_at (pragma_tok->location,
35876 "%<#pragma omp target exit data%> must contain at least "
35877 "one %<map%> clause");
35878 return NULL_TREE;
35879 }
35880
35881 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35882 TREE_TYPE (stmt) = void_type_node;
35883 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35884 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35885 return add_stmt (stmt);
35886}
35887
35888/* OpenMP 4.0:
35889 # pragma omp target update target-update-clause[optseq] new-line */
35890
35891#define OMP_TARGET_UPDATE_CLAUSE_MASK \
35892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35897 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35898
35899static bool
35900cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35901 enum pragma_context context)
35902{
35903 if (context == pragma_stmt)
35904 {
35905 error_at (pragma_tok->location,
35906 "%<#pragma %s%> may only be used in compound statements",
35907 "omp target update");
35908 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35909 return false;
35910 }
35911
35912 tree clauses
35913 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35914 "#pragma omp target update", pragma_tok);
35915 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35916 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35917 {
35918 error_at (pragma_tok->location,
35919 "%<#pragma omp target update%> must contain at least one "
35920 "%<from%> or %<to%> clauses");
35921 return false;
35922 }
35923
35924 tree stmt = make_node (OMP_TARGET_UPDATE);
35925 TREE_TYPE (stmt) = void_type_node;
35926 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35927 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35928 add_stmt (stmt);
35929 return false;
35930}
35931
35932/* OpenMP 4.0:
35933 # pragma omp target target-clause[optseq] new-line
35934 structured-block */
35935
35936#define OMP_TARGET_CLAUSE_MASK \
35937 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35946
35947static bool
35948cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35949 enum pragma_context context, bool *if_p)
35950{
35951 tree *pc = NULL, stmt;
35952
35953 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35954 {
35955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35956 const char *p = IDENTIFIER_POINTER (id);
35957 enum tree_code ccode = ERROR_MARK;
35958
35959 if (strcmp (p, "teams") == 0)
35960 ccode = OMP_TEAMS;
35961 else if (strcmp (p, "parallel") == 0)
35962 ccode = OMP_PARALLEL;
35963 else if (strcmp (p, "simd") == 0)
35964 ccode = OMP_SIMD;
35965 if (ccode != ERROR_MARK)
35966 {
35967 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35968 char p_name[sizeof ("#pragma omp target teams distribute "
35969 "parallel for simd")];
35970
35971 cp_lexer_consume_token (parser->lexer);
35972 strcpy (p_name, "#pragma omp target");
35973 if (!flag_openmp) /* flag_openmp_simd */
35974 {
35975 tree stmt;
35976 switch (ccode)
35977 {
35978 case OMP_TEAMS:
35979 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35980 OMP_TARGET_CLAUSE_MASK,
35981 cclauses, if_p);
35982 break;
35983 case OMP_PARALLEL:
35984 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35985 OMP_TARGET_CLAUSE_MASK,
35986 cclauses, if_p);
35987 break;
35988 case OMP_SIMD:
35989 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35990 OMP_TARGET_CLAUSE_MASK,
35991 cclauses, if_p);
35992 break;
35993 default:
35994 gcc_unreachable ();
35995 }
35996 return stmt != NULL_TREE;
35997 }
35998 keep_next_level (true);
35999 tree sb = begin_omp_structured_block (), ret;
36000 unsigned save = cp_parser_begin_omp_structured_block (parser);
36001 switch (ccode)
36002 {
36003 case OMP_TEAMS:
36004 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36005 OMP_TARGET_CLAUSE_MASK, cclauses,
36006 if_p);
36007 break;
36008 case OMP_PARALLEL:
36009 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36010 OMP_TARGET_CLAUSE_MASK, cclauses,
36011 if_p);
36012 break;
36013 case OMP_SIMD:
36014 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36015 OMP_TARGET_CLAUSE_MASK, cclauses,
36016 if_p);
36017 break;
36018 default:
36019 gcc_unreachable ();
36020 }
36021 cp_parser_end_omp_structured_block (parser, save);
36022 tree body = finish_omp_structured_block (sb);
36023 if (ret == NULL_TREE)
36024 return false;
36025 if (ccode == OMP_TEAMS && !processing_template_decl)
36026 {
36027 /* For combined target teams, ensure the num_teams and
36028 thread_limit clause expressions are evaluated on the host,
36029 before entering the target construct. */
36030 tree c;
36031 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36032 c; c = OMP_CLAUSE_CHAIN (c))
36033 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36034 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36035 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36036 {
36037 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36038 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36039 if (expr == error_mark_node)
36040 continue;
36041 tree tmp = TARGET_EXPR_SLOT (expr);
36042 add_stmt (expr);
36043 OMP_CLAUSE_OPERAND (c, 0) = expr;
36044 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36045 OMP_CLAUSE_FIRSTPRIVATE);
36046 OMP_CLAUSE_DECL (tc) = tmp;
36047 OMP_CLAUSE_CHAIN (tc)
36048 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36049 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36050 }
36051 }
36052 tree stmt = make_node (OMP_TARGET);
36053 TREE_TYPE (stmt) = void_type_node;
36054 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36055 OMP_TARGET_BODY (stmt) = body;
36056 OMP_TARGET_COMBINED (stmt) = 1;
36057 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36058 add_stmt (stmt);
36059 pc = &OMP_TARGET_CLAUSES (stmt);
36060 goto check_clauses;
36061 }
36062 else if (!flag_openmp) /* flag_openmp_simd */
36063 {
36064 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36065 return false;
36066 }
36067 else if (strcmp (p, "data") == 0)
36068 {
36069 cp_lexer_consume_token (parser->lexer);
36070 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36071 return true;
36072 }
36073 else if (strcmp (p, "enter") == 0)
36074 {
36075 cp_lexer_consume_token (parser->lexer);
36076 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36077 return false;
36078 }
36079 else if (strcmp (p, "exit") == 0)
36080 {
36081 cp_lexer_consume_token (parser->lexer);
36082 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36083 return false;
36084 }
36085 else if (strcmp (p, "update") == 0)
36086 {
36087 cp_lexer_consume_token (parser->lexer);
36088 return cp_parser_omp_target_update (parser, pragma_tok, context);
36089 }
36090 }
36091 if (!flag_openmp) /* flag_openmp_simd */
36092 {
36093 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36094 return false;
36095 }
36096
36097 stmt = make_node (OMP_TARGET);
36098 TREE_TYPE (stmt) = void_type_node;
36099
36100 OMP_TARGET_CLAUSES (stmt)
36101 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36102 "#pragma omp target", pragma_tok);
36103 pc = &OMP_TARGET_CLAUSES (stmt);
36104 keep_next_level (true);
36105 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36106
36107 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36108 add_stmt (stmt);
36109
36110check_clauses:
36111 while (*pc)
36112 {
36113 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36114 switch (OMP_CLAUSE_MAP_KIND (*pc))
36115 {
36116 case GOMP_MAP_TO:
36117 case GOMP_MAP_ALWAYS_TO:
36118 case GOMP_MAP_FROM:
36119 case GOMP_MAP_ALWAYS_FROM:
36120 case GOMP_MAP_TOFROM:
36121 case GOMP_MAP_ALWAYS_TOFROM:
36122 case GOMP_MAP_ALLOC:
36123 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36124 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36125 case GOMP_MAP_ALWAYS_POINTER:
36126 break;
36127 default:
36128 error_at (OMP_CLAUSE_LOCATION (*pc),
36129 "%<#pragma omp target%> with map-type other "
36130 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36131 "on %<map%> clause");
36132 *pc = OMP_CLAUSE_CHAIN (*pc);
36133 continue;
36134 }
36135 pc = &OMP_CLAUSE_CHAIN (*pc);
36136 }
36137 return true;
36138}
36139
36140/* OpenACC 2.0:
36141 # pragma acc cache (variable-list) new-line
36142*/
36143
36144static tree
36145cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36146{
36147 tree stmt, clauses;
36148
36149 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36150 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36151
36152 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36153
36154 stmt = make_node (OACC_CACHE);
36155 TREE_TYPE (stmt) = void_type_node;
36156 OACC_CACHE_CLAUSES (stmt) = clauses;
36157 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36158 add_stmt (stmt);
36159
36160 return stmt;
36161}
36162
36163/* OpenACC 2.0:
36164 # pragma acc data oacc-data-clause[optseq] new-line
36165 structured-block */
36166
36167#define OACC_DATA_CLAUSE_MASK \
36168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36179
36180static tree
36181cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36182{
36183 tree stmt, clauses, block;
36184 unsigned int save;
36185
36186 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36187 "#pragma acc data", pragma_tok);
36188
36189 block = begin_omp_parallel ();
36190 save = cp_parser_begin_omp_structured_block (parser);
36191 cp_parser_statement (parser, NULL_TREE, false, if_p);
36192 cp_parser_end_omp_structured_block (parser, save);
36193 stmt = finish_oacc_data (clauses, block);
36194 return stmt;
36195}
36196
36197/* OpenACC 2.0:
36198 # pragma acc host_data <clauses> new-line
36199 structured-block */
36200
36201#define OACC_HOST_DATA_CLAUSE_MASK \
36202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36203
36204static tree
36205cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36206{
36207 tree stmt, clauses, block;
36208 unsigned int save;
36209
36210 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36211 "#pragma acc host_data", pragma_tok);
36212
36213 block = begin_omp_parallel ();
36214 save = cp_parser_begin_omp_structured_block (parser);
36215 cp_parser_statement (parser, NULL_TREE, false, if_p);
36216 cp_parser_end_omp_structured_block (parser, save);
36217 stmt = finish_oacc_host_data (clauses, block);
36218 return stmt;
36219}
36220
36221/* OpenACC 2.0:
36222 # pragma acc declare oacc-data-clause[optseq] new-line
36223*/
36224
36225#define OACC_DECLARE_CLAUSE_MASK \
36226 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36238
36239static tree
36240cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36241{
36242 tree clauses, stmt;
36243 bool error = false;
36244
36245 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36246 "#pragma acc declare", pragma_tok, true);
36247
36248
36249 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36250 {
36251 error_at (pragma_tok->location,
36252 "no valid clauses specified in %<#pragma acc declare%>");
36253 return NULL_TREE;
36254 }
36255
36256 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36257 {
36258 location_t loc = OMP_CLAUSE_LOCATION (t);
36259 tree decl = OMP_CLAUSE_DECL (t);
36260 if (!DECL_P (decl))
36261 {
36262 error_at (loc, "array section in %<#pragma acc declare%>");
36263 error = true;
36264 continue;
36265 }
36266 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36267 switch (OMP_CLAUSE_MAP_KIND (t))
36268 {
36269 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36270 case GOMP_MAP_FORCE_ALLOC:
36271 case GOMP_MAP_FORCE_TO:
36272 case GOMP_MAP_FORCE_DEVICEPTR:
36273 case GOMP_MAP_DEVICE_RESIDENT:
36274 break;
36275
36276 case GOMP_MAP_LINK:
36277 if (!global_bindings_p ()
36278 && (TREE_STATIC (decl)
36279 || !DECL_EXTERNAL (decl)))
36280 {
36281 error_at (loc,
36282 "%qD must be a global variable in "
36283 "%<#pragma acc declare link%>",
36284 decl);
36285 error = true;
36286 continue;
36287 }
36288 break;
36289
36290 default:
36291 if (global_bindings_p ())
36292 {
36293 error_at (loc, "invalid OpenACC clause at file scope");
36294 error = true;
36295 continue;
36296 }
36297 if (DECL_EXTERNAL (decl))
36298 {
36299 error_at (loc,
36300 "invalid use of %<extern%> variable %qD "
36301 "in %<#pragma acc declare%>", decl);
36302 error = true;
36303 continue;
36304 }
36305 else if (TREE_PUBLIC (decl))
36306 {
36307 error_at (loc,
36308 "invalid use of %<global%> variable %qD "
36309 "in %<#pragma acc declare%>", decl);
36310 error = true;
36311 continue;
36312 }
36313 break;
36314 }
36315
36316 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36317 || lookup_attribute ("omp declare target link",
36318 DECL_ATTRIBUTES (decl)))
36319 {
36320 error_at (loc, "variable %qD used more than once with "
36321 "%<#pragma acc declare%>", decl);
36322 error = true;
36323 continue;
36324 }
36325
36326 if (!error)
36327 {
36328 tree id;
36329
36330 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36331 id = get_identifier ("omp declare target link");
36332 else
36333 id = get_identifier ("omp declare target");
36334
36335 DECL_ATTRIBUTES (decl)
36336 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36337 if (global_bindings_p ())
36338 {
36339 symtab_node *node = symtab_node::get (decl);
36340 if (node != NULL)
36341 {
36342 node->offloadable = 1;
36343 if (ENABLE_OFFLOADING)
36344 {
36345 g->have_offload = true;
36346 if (is_a <varpool_node *> (node))
36347 vec_safe_push (offload_vars, decl);
36348 }
36349 }
36350 }
36351 }
36352 }
36353
36354 if (error || global_bindings_p ())
36355 return NULL_TREE;
36356
36357 stmt = make_node (OACC_DECLARE);
36358 TREE_TYPE (stmt) = void_type_node;
36359 OACC_DECLARE_CLAUSES (stmt) = clauses;
36360 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36361
36362 add_stmt (stmt);
36363
36364 return NULL_TREE;
36365}
36366
36367/* OpenACC 2.0:
36368 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36369
36370 or
36371
36372 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36373
36374 LOC is the location of the #pragma token.
36375*/
36376
36377#define OACC_ENTER_DATA_CLAUSE_MASK \
36378 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36385
36386#define OACC_EXIT_DATA_CLAUSE_MASK \
36387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36392
36393static tree
36394cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36395 bool enter)
36396{
36397 location_t loc = pragma_tok->location;
36398 tree stmt, clauses;
36399 const char *p = "";
36400
36401 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36402 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36403
36404 if (strcmp (p, "data") != 0)
36405 {
36406 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36407 enter ? "enter" : "exit");
36408 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36409 return NULL_TREE;
36410 }
36411
36412 cp_lexer_consume_token (parser->lexer);
36413
36414 if (enter)
36415 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36416 "#pragma acc enter data", pragma_tok);
36417 else
36418 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36419 "#pragma acc exit data", pragma_tok);
36420
36421 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36422 {
36423 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36424 enter ? "enter" : "exit");
36425 return NULL_TREE;
36426 }
36427
36428 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36429 TREE_TYPE (stmt) = void_type_node;
36430 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36431 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36432 add_stmt (stmt);
36433 return stmt;
36434}
36435
36436/* OpenACC 2.0:
36437 # pragma acc loop oacc-loop-clause[optseq] new-line
36438 structured-block */
36439
36440#define OACC_LOOP_CLAUSE_MASK \
36441 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36451
36452static tree
36453cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36454 omp_clause_mask mask, tree *cclauses, bool *if_p)
36455{
36456 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36457
36458 strcat (p_name, " loop");
36459 mask |= OACC_LOOP_CLAUSE_MASK;
36460
36461 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36462 cclauses == NULL);
36463 if (cclauses)
36464 {
36465 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36466 if (*cclauses)
36467 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36468 if (clauses)
36469 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36470 }
36471
36472 tree block = begin_omp_structured_block ();
36473 int save = cp_parser_begin_omp_structured_block (parser);
36474 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36475 cp_parser_end_omp_structured_block (parser, save);
36476 add_stmt (finish_omp_structured_block (block));
36477
36478 return stmt;
36479}
36480
36481/* OpenACC 2.0:
36482 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36483 structured-block
36484
36485 or
36486
36487 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36488 structured-block
36489*/
36490
36491#define OACC_KERNELS_CLAUSE_MASK \
36492 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36506
36507#define OACC_PARALLEL_CLAUSE_MASK \
36508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36528
36529static tree
36530cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36531 char *p_name, bool *if_p)
36532{
36533 omp_clause_mask mask;
36534 enum tree_code code;
36535 switch (cp_parser_pragma_kind (pragma_tok))
36536 {
36537 case PRAGMA_OACC_KERNELS:
36538 strcat (p_name, " kernels");
36539 mask = OACC_KERNELS_CLAUSE_MASK;
36540 code = OACC_KERNELS;
36541 break;
36542 case PRAGMA_OACC_PARALLEL:
36543 strcat (p_name, " parallel");
36544 mask = OACC_PARALLEL_CLAUSE_MASK;
36545 code = OACC_PARALLEL;
36546 break;
36547 default:
36548 gcc_unreachable ();
36549 }
36550
36551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36552 {
36553 const char *p
36554 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36555 if (strcmp (p, "loop") == 0)
36556 {
36557 cp_lexer_consume_token (parser->lexer);
36558 tree block = begin_omp_parallel ();
36559 tree clauses;
36560 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36561 if_p);
36562 return finish_omp_construct (code, block, clauses);
36563 }
36564 }
36565
36566 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36567
36568 tree block = begin_omp_parallel ();
36569 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36570 cp_parser_statement (parser, NULL_TREE, false, if_p);
36571 cp_parser_end_omp_structured_block (parser, save);
36572 return finish_omp_construct (code, block, clauses);
36573}
36574
36575/* OpenACC 2.0:
36576 # pragma acc update oacc-update-clause[optseq] new-line
36577*/
36578
36579#define OACC_UPDATE_CLAUSE_MASK \
36580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36586
36587static tree
36588cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36589{
36590 tree stmt, clauses;
36591
36592 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36593 "#pragma acc update", pragma_tok);
36594
36595 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36596 {
36597 error_at (pragma_tok->location,
36598 "%<#pragma acc update%> must contain at least one "
36599 "%<device%> or %<host%> or %<self%> clause");
36600 return NULL_TREE;
36601 }
36602
36603 stmt = make_node (OACC_UPDATE);
36604 TREE_TYPE (stmt) = void_type_node;
36605 OACC_UPDATE_CLAUSES (stmt) = clauses;
36606 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36607 add_stmt (stmt);
36608 return stmt;
36609}
36610
36611/* OpenACC 2.0:
36612 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36613
36614 LOC is the location of the #pragma token.
36615*/
36616
36617#define OACC_WAIT_CLAUSE_MASK \
36618 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36619
36620static tree
36621cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36622{
36623 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36624 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36625
36626 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36627 list = cp_parser_oacc_wait_list (parser, loc, list);
36628
36629 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36630 "#pragma acc wait", pragma_tok);
36631
36632 stmt = c_finish_oacc_wait (loc, list, clauses);
36633 stmt = finish_expr_stmt (stmt);
36634
36635 return stmt;
36636}
36637
36638/* OpenMP 4.0:
36639 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36640
36641#define OMP_DECLARE_SIMD_CLAUSE_MASK \
36642 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36648
36649static void
36650cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36651 enum pragma_context context)
36652{
36653 bool first_p = parser->omp_declare_simd == NULL;
36654 cp_omp_declare_simd_data data;
36655 if (first_p)
36656 {
36657 data.error_seen = false;
36658 data.fndecl_seen = false;
36659 data.tokens = vNULL;
36660 data.clauses = NULL_TREE;
36661 /* It is safe to take the address of a local variable; it will only be
36662 used while this scope is live. */
36663 parser->omp_declare_simd = &data;
36664 }
36665
36666 /* Store away all pragma tokens. */
36667 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36668 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36669 cp_lexer_consume_token (parser->lexer);
36670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36671 parser->omp_declare_simd->error_seen = true;
36672 cp_parser_require_pragma_eol (parser, pragma_tok);
36673 struct cp_token_cache *cp
36674 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36675 parser->omp_declare_simd->tokens.safe_push (cp);
36676
36677 if (first_p)
36678 {
36679 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36680 cp_parser_pragma (parser, context, NULL);
36681 switch (context)
36682 {
36683 case pragma_external:
36684 cp_parser_declaration (parser);
36685 break;
36686 case pragma_member:
36687 cp_parser_member_declaration (parser);
36688 break;
36689 case pragma_objc_icode:
36690 cp_parser_block_declaration (parser, /*statement_p=*/false);
36691 break;
36692 default:
36693 cp_parser_declaration_statement (parser);
36694 break;
36695 }
36696 if (parser->omp_declare_simd
36697 && !parser->omp_declare_simd->error_seen
36698 && !parser->omp_declare_simd->fndecl_seen)
36699 error_at (pragma_tok->location,
36700 "%<#pragma omp declare simd%> not immediately followed by "
36701 "function declaration or definition");
36702 data.tokens.release ();
36703 parser->omp_declare_simd = NULL;
36704 }
36705}
36706
36707/* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36708 This function is modelled similar to the late parsing of omp declare
36709 simd. */
36710
36711static tree
36712cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36713{
36714 struct cp_token_cache *ce;
36715 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36716 int ii = 0;
36717
36718 if (parser->omp_declare_simd != NULL
36719 || lookup_attribute ("simd", attrs))
36720 {
36721 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36722 "used in the same function marked as a Cilk Plus SIMD-enabled "
36723 "function");
36724 parser->cilk_simd_fn_info->tokens.release ();
36725 XDELETE (parser->cilk_simd_fn_info);
36726 parser->cilk_simd_fn_info = NULL;
36727 return attrs;
36728 }
36729 if (!info->error_seen && info->fndecl_seen)
36730 {
36731 error ("vector attribute not immediately followed by a single function"
36732 " declaration or definition");
36733 info->error_seen = true;
36734 }
36735 if (info->error_seen)
36736 return attrs;
36737
36738 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36739 {
36740 tree c, cl;
36741
36742 cp_parser_push_lexer_for_tokens (parser, ce);
36743 parser->lexer->in_pragma = true;
36744 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36745 "SIMD-enabled functions attribute",
36746 NULL);
36747 cp_parser_pop_lexer (parser);
36748 if (cl)
36749 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36750
36751 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36752 TREE_CHAIN (c) = attrs;
36753 attrs = c;
36754
36755 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36756 TREE_CHAIN (c) = attrs;
36757 if (processing_template_decl)
36758 ATTR_IS_DEPENDENT (c) = 1;
36759 attrs = c;
36760 }
36761 info->fndecl_seen = true;
36762 parser->cilk_simd_fn_info->tokens.release ();
36763 XDELETE (parser->cilk_simd_fn_info);
36764 parser->cilk_simd_fn_info = NULL;
36765 return attrs;
36766}
36767
36768/* Finalize #pragma omp declare simd clauses after direct declarator has
36769 been parsed, and put that into "omp declare simd" attribute. */
36770
36771static tree
36772cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36773{
36774 struct cp_token_cache *ce;
36775 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36776 int i;
36777
36778 if (!data->error_seen && data->fndecl_seen)
36779 {
36780 error ("%<#pragma omp declare simd%> not immediately followed by "
36781 "a single function declaration or definition");
36782 data->error_seen = true;
36783 }
36784 if (data->error_seen)
36785 return attrs;
36786
36787 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36788 {
36789 tree c, cl;
36790
36791 cp_parser_push_lexer_for_tokens (parser, ce);
36792 parser->lexer->in_pragma = true;
36793 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36794 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36795 cp_lexer_consume_token (parser->lexer);
36796 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36797 "#pragma omp declare simd", pragma_tok);
36798 cp_parser_pop_lexer (parser);
36799 if (cl)
36800 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36801 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36802 TREE_CHAIN (c) = attrs;
36803 if (processing_template_decl)
36804 ATTR_IS_DEPENDENT (c) = 1;
36805 attrs = c;
36806 }
36807
36808 data->fndecl_seen = true;
36809 return attrs;
36810}
36811
36812
36813/* OpenMP 4.0:
36814 # pragma omp declare target new-line
36815 declarations and definitions
36816 # pragma omp end declare target new-line
36817
36818 OpenMP 4.5:
36819 # pragma omp declare target ( extended-list ) new-line
36820
36821 # pragma omp declare target declare-target-clauses[seq] new-line */
36822
36823#define OMP_DECLARE_TARGET_CLAUSE_MASK \
36824 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36826
36827static void
36828cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36829{
36830 tree clauses = NULL_TREE;
36831 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36832 clauses
36833 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36834 "#pragma omp declare target", pragma_tok);
36835 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36836 {
36837 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36838 clauses);
36839 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36840 cp_parser_require_pragma_eol (parser, pragma_tok);
36841 }
36842 else
36843 {
36844 cp_parser_require_pragma_eol (parser, pragma_tok);
36845 scope_chain->omp_declare_target_attribute++;
36846 return;
36847 }
36848 if (scope_chain->omp_declare_target_attribute)
36849 error_at (pragma_tok->location,
36850 "%<#pragma omp declare target%> with clauses in between "
36851 "%<#pragma omp declare target%> without clauses and "
36852 "%<#pragma omp end declare target%>");
36853 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36854 {
36855 tree t = OMP_CLAUSE_DECL (c), id;
36856 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36857 tree at2 = lookup_attribute ("omp declare target link",
36858 DECL_ATTRIBUTES (t));
36859 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36860 {
36861 id = get_identifier ("omp declare target link");
36862 std::swap (at1, at2);
36863 }
36864 else
36865 id = get_identifier ("omp declare target");
36866 if (at2)
36867 {
36868 error_at (OMP_CLAUSE_LOCATION (c),
36869 "%qD specified both in declare target %<link%> and %<to%>"
36870 " clauses", t);
36871 continue;
36872 }
36873 if (!at1)
36874 {
36875 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36876 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36877 continue;
36878
36879 symtab_node *node = symtab_node::get (t);
36880 if (node != NULL)
36881 {
36882 node->offloadable = 1;
36883 if (ENABLE_OFFLOADING)
36884 {
36885 g->have_offload = true;
36886 if (is_a <varpool_node *> (node))
36887 vec_safe_push (offload_vars, t);
36888 }
36889 }
36890 }
36891 }
36892}
36893
36894static void
36895cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36896{
36897 const char *p = "";
36898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36899 {
36900 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36901 p = IDENTIFIER_POINTER (id);
36902 }
36903 if (strcmp (p, "declare") == 0)
36904 {
36905 cp_lexer_consume_token (parser->lexer);
36906 p = "";
36907 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36908 {
36909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36910 p = IDENTIFIER_POINTER (id);
36911 }
36912 if (strcmp (p, "target") == 0)
36913 cp_lexer_consume_token (parser->lexer);
36914 else
36915 {
36916 cp_parser_error (parser, "expected %<target%>");
36917 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36918 return;
36919 }
36920 }
36921 else
36922 {
36923 cp_parser_error (parser, "expected %<declare%>");
36924 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36925 return;
36926 }
36927 cp_parser_require_pragma_eol (parser, pragma_tok);
36928 if (!scope_chain->omp_declare_target_attribute)
36929 error_at (pragma_tok->location,
36930 "%<#pragma omp end declare target%> without corresponding "
36931 "%<#pragma omp declare target%>");
36932 else
36933 scope_chain->omp_declare_target_attribute--;
36934}
36935
36936/* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36937 expression and optional initializer clause of
36938 #pragma omp declare reduction. We store the expression(s) as
36939 either 3, 6 or 7 special statements inside of the artificial function's
36940 body. The first two statements are DECL_EXPRs for the artificial
36941 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36942 expression that uses those variables.
36943 If there was any INITIALIZER clause, this is followed by further statements,
36944 the fourth and fifth statements are DECL_EXPRs for the artificial
36945 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36946 constructor variant (first token after open paren is not omp_priv),
36947 then the sixth statement is a statement with the function call expression
36948 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36949 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36950 to initialize the OMP_PRIV artificial variable and there is seventh
36951 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36952
36953static bool
36954cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36955{
36956 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36957 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36958 type = TREE_TYPE (type);
36959 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36960 DECL_ARTIFICIAL (omp_out) = 1;
36961 pushdecl (omp_out);
36962 add_decl_expr (omp_out);
36963 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36964 DECL_ARTIFICIAL (omp_in) = 1;
36965 pushdecl (omp_in);
36966 add_decl_expr (omp_in);
36967 tree combiner;
36968 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36969
36970 keep_next_level (true);
36971 tree block = begin_omp_structured_block ();
36972 combiner = cp_parser_expression (parser);
36973 finish_expr_stmt (combiner);
36974 block = finish_omp_structured_block (block);
36975 add_stmt (block);
36976
36977 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36978 return false;
36979
36980 const char *p = "";
36981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36982 {
36983 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36984 p = IDENTIFIER_POINTER (id);
36985 }
36986
36987 if (strcmp (p, "initializer") == 0)
36988 {
36989 cp_lexer_consume_token (parser->lexer);
36990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36991 return false;
36992
36993 p = "";
36994 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36995 {
36996 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36997 p = IDENTIFIER_POINTER (id);
36998 }
36999
37000 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37001 DECL_ARTIFICIAL (omp_priv) = 1;
37002 pushdecl (omp_priv);
37003 add_decl_expr (omp_priv);
37004 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37005 DECL_ARTIFICIAL (omp_orig) = 1;
37006 pushdecl (omp_orig);
37007 add_decl_expr (omp_orig);
37008
37009 keep_next_level (true);
37010 block = begin_omp_structured_block ();
37011
37012 bool ctor = false;
37013 if (strcmp (p, "omp_priv") == 0)
37014 {
37015 bool is_direct_init, is_non_constant_init;
37016 ctor = true;
37017 cp_lexer_consume_token (parser->lexer);
37018 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37019 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37020 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37021 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37022 == CPP_CLOSE_PAREN
37023 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37024 == CPP_CLOSE_PAREN))
37025 {
37026 finish_omp_structured_block (block);
37027 error ("invalid initializer clause");
37028 return false;
37029 }
37030 initializer = cp_parser_initializer (parser, &is_direct_init,
37031 &is_non_constant_init);
37032 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37033 NULL_TREE, LOOKUP_ONLYCONVERTING);
37034 }
37035 else
37036 {
37037 cp_parser_parse_tentatively (parser);
37038 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37039 /*check_dependency_p=*/true,
37040 /*template_p=*/NULL,
37041 /*declarator_p=*/false,
37042 /*optional_p=*/false);
37043 vec<tree, va_gc> *args;
37044 if (fn_name == error_mark_node
37045 || cp_parser_error_occurred (parser)
37046 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37047 || ((args = cp_parser_parenthesized_expression_list
37048 (parser, non_attr, /*cast_p=*/false,
37049 /*allow_expansion_p=*/true,
37050 /*non_constant_p=*/NULL)),
37051 cp_parser_error_occurred (parser)))
37052 {
37053 finish_omp_structured_block (block);
37054 cp_parser_abort_tentative_parse (parser);
37055 cp_parser_error (parser, "expected id-expression (arguments)");
37056 return false;
37057 }
37058 unsigned int i;
37059 tree arg;
37060 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37061 if (arg == omp_priv
37062 || (TREE_CODE (arg) == ADDR_EXPR
37063 && TREE_OPERAND (arg, 0) == omp_priv))
37064 break;
37065 cp_parser_abort_tentative_parse (parser);
37066 if (arg == NULL_TREE)
37067 error ("one of the initializer call arguments should be %<omp_priv%>"
37068 " or %<&omp_priv%>");
37069 initializer = cp_parser_postfix_expression (parser, false, false, false,
37070 false, NULL);
37071 finish_expr_stmt (initializer);
37072 }
37073
37074 block = finish_omp_structured_block (block);
37075 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37076 add_stmt (block);
37077
37078 if (ctor)
37079 add_decl_expr (omp_orig);
37080
37081 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37082 return false;
37083 }
37084
37085 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37086 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37087
37088 return true;
37089}
37090
37091/* OpenMP 4.0
37092 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37093 initializer-clause[opt] new-line
37094
37095 initializer-clause:
37096 initializer (omp_priv initializer)
37097 initializer (function-name (argument-list)) */
37098
37099static void
37100cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37101 enum pragma_context)
37102{
37103 auto_vec<tree> types;
37104 enum tree_code reduc_code = ERROR_MARK;
37105 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37106 unsigned int i;
37107 cp_token *first_token;
37108 cp_token_cache *cp;
37109 int errs;
37110 void *p;
37111
37112 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37113 p = obstack_alloc (&declarator_obstack, 0);
37114
37115 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37116 goto fail;
37117
37118 switch (cp_lexer_peek_token (parser->lexer)->type)
37119 {
37120 case CPP_PLUS:
37121 reduc_code = PLUS_EXPR;
37122 break;
37123 case CPP_MULT:
37124 reduc_code = MULT_EXPR;
37125 break;
37126 case CPP_MINUS:
37127 reduc_code = MINUS_EXPR;
37128 break;
37129 case CPP_AND:
37130 reduc_code = BIT_AND_EXPR;
37131 break;
37132 case CPP_XOR:
37133 reduc_code = BIT_XOR_EXPR;
37134 break;
37135 case CPP_OR:
37136 reduc_code = BIT_IOR_EXPR;
37137 break;
37138 case CPP_AND_AND:
37139 reduc_code = TRUTH_ANDIF_EXPR;
37140 break;
37141 case CPP_OR_OR:
37142 reduc_code = TRUTH_ORIF_EXPR;
37143 break;
37144 case CPP_NAME:
37145 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37146 break;
37147 default:
37148 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37149 "%<|%>, %<&&%>, %<||%> or identifier");
37150 goto fail;
37151 }
37152
37153 if (reduc_code != ERROR_MARK)
37154 cp_lexer_consume_token (parser->lexer);
37155
37156 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37157 if (reduc_id == error_mark_node)
37158 goto fail;
37159
37160 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37161 goto fail;
37162
37163 /* Types may not be defined in declare reduction type list. */
37164 const char *saved_message;
37165 saved_message = parser->type_definition_forbidden_message;
37166 parser->type_definition_forbidden_message
37167 = G_("types may not be defined in declare reduction type list");
37168 bool saved_colon_corrects_to_scope_p;
37169 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37170 parser->colon_corrects_to_scope_p = false;
37171 bool saved_colon_doesnt_start_class_def_p;
37172 saved_colon_doesnt_start_class_def_p
37173 = parser->colon_doesnt_start_class_def_p;
37174 parser->colon_doesnt_start_class_def_p = true;
37175
37176 while (true)
37177 {
37178 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37179 type = cp_parser_type_id (parser);
37180 if (type == error_mark_node)
37181 ;
37182 else if (ARITHMETIC_TYPE_P (type)
37183 && (orig_reduc_id == NULL_TREE
37184 || (TREE_CODE (type) != COMPLEX_TYPE
37185 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37186 "min") == 0
37187 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37188 "max") == 0))))
37189 error_at (loc, "predeclared arithmetic type %qT in "
37190 "%<#pragma omp declare reduction%>", type);
37191 else if (TREE_CODE (type) == FUNCTION_TYPE
37192 || TREE_CODE (type) == METHOD_TYPE
37193 || TREE_CODE (type) == ARRAY_TYPE)
37194 error_at (loc, "function or array type %qT in "
37195 "%<#pragma omp declare reduction%>", type);
37196 else if (TREE_CODE (type) == REFERENCE_TYPE)
37197 error_at (loc, "reference type %qT in "
37198 "%<#pragma omp declare reduction%>", type);
37199 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37200 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37201 "%<#pragma omp declare reduction%>", type);
37202 else
37203 types.safe_push (type);
37204
37205 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37206 cp_lexer_consume_token (parser->lexer);
37207 else
37208 break;
37209 }
37210
37211 /* Restore the saved message. */
37212 parser->type_definition_forbidden_message = saved_message;
37213 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37214 parser->colon_doesnt_start_class_def_p
37215 = saved_colon_doesnt_start_class_def_p;
37216
37217 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37218 || types.is_empty ())
37219 {
37220 fail:
37221 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37222 goto done;
37223 }
37224
37225 first_token = cp_lexer_peek_token (parser->lexer);
37226 cp = NULL;
37227 errs = errorcount;
37228 FOR_EACH_VEC_ELT (types, i, type)
37229 {
37230 tree fntype
37231 = build_function_type_list (void_type_node,
37232 cp_build_reference_type (type, false),
37233 NULL_TREE);
37234 tree this_reduc_id = reduc_id;
37235 if (!dependent_type_p (type))
37236 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37237 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37238 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37239 DECL_ARTIFICIAL (fndecl) = 1;
37240 DECL_EXTERNAL (fndecl) = 1;
37241 DECL_DECLARED_INLINE_P (fndecl) = 1;
37242 DECL_IGNORED_P (fndecl) = 1;
37243 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37244 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37245 DECL_ATTRIBUTES (fndecl)
37246 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37247 DECL_ATTRIBUTES (fndecl));
37248 if (processing_template_decl)
37249 fndecl = push_template_decl (fndecl);
37250 bool block_scope = false;
37251 tree block = NULL_TREE;
37252 if (current_function_decl)
37253 {
37254 block_scope = true;
37255 DECL_CONTEXT (fndecl) = global_namespace;
37256 if (!processing_template_decl)
37257 pushdecl (fndecl);
37258 }
37259 else if (current_class_type)
37260 {
37261 if (cp == NULL)
37262 {
37263 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37264 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37265 cp_lexer_consume_token (parser->lexer);
37266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37267 goto fail;
37268 cp = cp_token_cache_new (first_token,
37269 cp_lexer_peek_nth_token (parser->lexer,
37270 2));
37271 }
37272 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37273 finish_member_declaration (fndecl);
37274 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37275 DECL_PENDING_INLINE_P (fndecl) = 1;
37276 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37277 continue;
37278 }
37279 else
37280 {
37281 DECL_CONTEXT (fndecl) = current_namespace;
37282 pushdecl (fndecl);
37283 }
37284 if (!block_scope)
37285 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37286 else
37287 block = begin_omp_structured_block ();
37288 if (cp)
37289 {
37290 cp_parser_push_lexer_for_tokens (parser, cp);
37291 parser->lexer->in_pragma = true;
37292 }
37293 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37294 {
37295 if (!block_scope)
37296 finish_function (0);
37297 else
37298 DECL_CONTEXT (fndecl) = current_function_decl;
37299 if (cp)
37300 cp_parser_pop_lexer (parser);
37301 goto fail;
37302 }
37303 if (cp)
37304 cp_parser_pop_lexer (parser);
37305 if (!block_scope)
37306 finish_function (0);
37307 else
37308 {
37309 DECL_CONTEXT (fndecl) = current_function_decl;
37310 block = finish_omp_structured_block (block);
37311 if (TREE_CODE (block) == BIND_EXPR)
37312 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37313 else if (TREE_CODE (block) == STATEMENT_LIST)
37314 DECL_SAVED_TREE (fndecl) = block;
37315 if (processing_template_decl)
37316 add_decl_expr (fndecl);
37317 }
37318 cp_check_omp_declare_reduction (fndecl);
37319 if (cp == NULL && types.length () > 1)
37320 cp = cp_token_cache_new (first_token,
37321 cp_lexer_peek_nth_token (parser->lexer, 2));
37322 if (errs != errorcount)
37323 break;
37324 }
37325
37326 cp_parser_require_pragma_eol (parser, pragma_tok);
37327
37328 done:
37329 /* Free any declarators allocated. */
37330 obstack_free (&declarator_obstack, p);
37331}
37332
37333/* OpenMP 4.0
37334 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37335 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37336 initializer-clause[opt] new-line
37337 #pragma omp declare target new-line */
37338
37339static bool
37340cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37341 enum pragma_context context)
37342{
37343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37344 {
37345 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37346 const char *p = IDENTIFIER_POINTER (id);
37347
37348 if (strcmp (p, "simd") == 0)
37349 {
37350 cp_lexer_consume_token (parser->lexer);
37351 cp_parser_omp_declare_simd (parser, pragma_tok,
37352 context);
37353 return true;
37354 }
37355 cp_ensure_no_omp_declare_simd (parser);
37356 if (strcmp (p, "reduction") == 0)
37357 {
37358 cp_lexer_consume_token (parser->lexer);
37359 cp_parser_omp_declare_reduction (parser, pragma_tok,
37360 context);
37361 return false;
37362 }
37363 if (!flag_openmp) /* flag_openmp_simd */
37364 {
37365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37366 return false;
37367 }
37368 if (strcmp (p, "target") == 0)
37369 {
37370 cp_lexer_consume_token (parser->lexer);
37371 cp_parser_omp_declare_target (parser, pragma_tok);
37372 return false;
37373 }
37374 }
37375 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37376 "or %<target%>");
37377 cp_parser_require_pragma_eol (parser, pragma_tok);
37378 return false;
37379}
37380
37381/* OpenMP 4.5:
37382 #pragma omp taskloop taskloop-clause[optseq] new-line
37383 for-loop
37384
37385 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37386 for-loop */
37387
37388#define OMP_TASKLOOP_CLAUSE_MASK \
37389 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37403
37404static tree
37405cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37406 char *p_name, omp_clause_mask mask, tree *cclauses,
37407 bool *if_p)
37408{
37409 tree clauses, sb, ret;
37410 unsigned int save;
37411 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37412
37413 strcat (p_name, " taskloop");
37414 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37415
37416 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37417 {
37418 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37419 const char *p = IDENTIFIER_POINTER (id);
37420
37421 if (strcmp (p, "simd") == 0)
37422 {
37423 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37424 if (cclauses == NULL)
37425 cclauses = cclauses_buf;
37426
37427 cp_lexer_consume_token (parser->lexer);
37428 if (!flag_openmp) /* flag_openmp_simd */
37429 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37430 cclauses, if_p);
37431 sb = begin_omp_structured_block ();
37432 save = cp_parser_begin_omp_structured_block (parser);
37433 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37434 cclauses, if_p);
37435 cp_parser_end_omp_structured_block (parser, save);
37436 tree body = finish_omp_structured_block (sb);
37437 if (ret == NULL)
37438 return ret;
37439 ret = make_node (OMP_TASKLOOP);
37440 TREE_TYPE (ret) = void_type_node;
37441 OMP_FOR_BODY (ret) = body;
37442 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37443 SET_EXPR_LOCATION (ret, loc);
37444 add_stmt (ret);
37445 return ret;
37446 }
37447 }
37448 if (!flag_openmp) /* flag_openmp_simd */
37449 {
37450 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37451 return NULL_TREE;
37452 }
37453
37454 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37455 cclauses == NULL);
37456 if (cclauses)
37457 {
37458 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37459 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37460 }
37461
37462 sb = begin_omp_structured_block ();
37463 save = cp_parser_begin_omp_structured_block (parser);
37464
37465 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37466 if_p);
37467
37468 cp_parser_end_omp_structured_block (parser, save);
37469 add_stmt (finish_omp_structured_block (sb));
37470
37471 return ret;
37472}
37473
37474
37475/* OpenACC 2.0:
37476 # pragma acc routine oacc-routine-clause[optseq] new-line
37477 function-definition
37478
37479 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37480*/
37481
37482#define OACC_ROUTINE_CLAUSE_MASK \
37483 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37487
37488
37489/* Parse the OpenACC routine pragma. This has an optional '( name )'
37490 component, which must resolve to a declared namespace-scope
37491 function. The clauses are either processed directly (for a named
37492 function), or defered until the immediatley following declaration
37493 is parsed. */
37494
37495static void
37496cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37497 enum pragma_context context)
37498{
37499 gcc_checking_assert (context == pragma_external);
37500 /* The checking for "another pragma following this one" in the "no optional
37501 '( name )'" case makes sure that we dont re-enter. */
37502 gcc_checking_assert (parser->oacc_routine == NULL);
37503
37504 cp_oacc_routine_data data;
37505 data.error_seen = false;
37506 data.fndecl_seen = false;
37507 data.tokens = vNULL;
37508 data.clauses = NULL_TREE;
37509 data.loc = pragma_tok->location;
37510 /* It is safe to take the address of a local variable; it will only be
37511 used while this scope is live. */
37512 parser->oacc_routine = &data;
37513
37514 /* Look for optional '( name )'. */
37515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37516 {
37517 cp_lexer_consume_token (parser->lexer); /* '(' */
37518
37519 /* We parse the name as an id-expression. If it resolves to
37520 anything other than a non-overloaded function at namespace
37521 scope, it's an error. */
37522 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37523 tree name = cp_parser_id_expression (parser,
37524 /*template_keyword_p=*/false,
37525 /*check_dependency_p=*/false,
37526 /*template_p=*/NULL,
37527 /*declarator_p=*/false,
37528 /*optional_p=*/false);
37529 tree decl = (identifier_p (name)
37530 ? cp_parser_lookup_name_simple (parser, name, name_loc)
37531 : name);
37532 if (name != error_mark_node && decl == error_mark_node)
37533 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37534
37535 if (decl == error_mark_node
37536 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37537 {
37538 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37539 parser->oacc_routine = NULL;
37540 return;
37541 }
37542
37543 data.clauses
37544 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37545 "#pragma acc routine",
37546 cp_lexer_peek_token (parser->lexer));
37547
37548 if (decl && is_overloaded_fn (decl)
37549 && (TREE_CODE (decl) != FUNCTION_DECL
37550 || DECL_FUNCTION_TEMPLATE_P (decl)))
37551 {
37552 error_at (name_loc,
37553 "%<#pragma acc routine%> names a set of overloads");
37554 parser->oacc_routine = NULL;
37555 return;
37556 }
37557
37558 /* Perhaps we should use the same rule as declarations in different
37559 namespaces? */
37560 if (!DECL_NAMESPACE_SCOPE_P (decl))
37561 {
37562 error_at (name_loc,
37563 "%qD does not refer to a namespace scope function", decl);
37564 parser->oacc_routine = NULL;
37565 return;
37566 }
37567
37568 if (TREE_CODE (decl) != FUNCTION_DECL)
37569 {
37570 error_at (name_loc, "%qD does not refer to a function", decl);
37571 parser->oacc_routine = NULL;
37572 return;
37573 }
37574
37575 cp_finalize_oacc_routine (parser, decl, false);
37576 parser->oacc_routine = NULL;
37577 }
37578 else /* No optional '( name )'. */
37579 {
37580 /* Store away all pragma tokens. */
37581 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37582 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37583 cp_lexer_consume_token (parser->lexer);
37584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37585 parser->oacc_routine->error_seen = true;
37586 cp_parser_require_pragma_eol (parser, pragma_tok);
37587 struct cp_token_cache *cp
37588 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37589 parser->oacc_routine->tokens.safe_push (cp);
37590
37591 /* Emit a helpful diagnostic if there's another pragma following this
37592 one. */
37593 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37594 {
37595 cp_ensure_no_oacc_routine (parser);
37596 data.tokens.release ();
37597 /* ..., and then just keep going. */
37598 return;
37599 }
37600
37601 /* We only have to consider the pragma_external case here. */
37602 cp_parser_declaration (parser);
37603 if (parser->oacc_routine
37604 && !parser->oacc_routine->fndecl_seen)
37605 cp_ensure_no_oacc_routine (parser);
37606 else
37607 parser->oacc_routine = NULL;
37608 data.tokens.release ();
37609 }
37610}
37611
37612/* Finalize #pragma acc routine clauses after direct declarator has
37613 been parsed. */
37614
37615static tree
37616cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37617{
37618 struct cp_token_cache *ce;
37619 cp_oacc_routine_data *data = parser->oacc_routine;
37620
37621 if (!data->error_seen && data->fndecl_seen)
37622 {
37623 error_at (data->loc,
37624 "%<#pragma acc routine%> not immediately followed by "
37625 "a single function declaration or definition");
37626 data->error_seen = true;
37627 }
37628 if (data->error_seen)
37629 return attrs;
37630
37631 gcc_checking_assert (data->tokens.length () == 1);
37632 ce = data->tokens[0];
37633
37634 cp_parser_push_lexer_for_tokens (parser, ce);
37635 parser->lexer->in_pragma = true;
37636 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37637
37638 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37639 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37640 parser->oacc_routine->clauses
37641 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37642 "#pragma acc routine", pragma_tok);
37643 cp_parser_pop_lexer (parser);
37644 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37645 fndecl_seen. */
37646
37647 return attrs;
37648}
37649
37650/* Apply any saved OpenACC routine clauses to a just-parsed
37651 declaration. */
37652
37653static void
37654cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37655{
37656 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37657 {
37658 /* Keep going if we're in error reporting mode. */
37659 if (parser->oacc_routine->error_seen
37660 || fndecl == error_mark_node)
37661 return;
37662
37663 if (parser->oacc_routine->fndecl_seen)
37664 {
37665 error_at (parser->oacc_routine->loc,
37666 "%<#pragma acc routine%> not immediately followed by"
37667 " a single function declaration or definition");
37668 parser->oacc_routine = NULL;
37669 return;
37670 }
37671 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37672 {
37673 cp_ensure_no_oacc_routine (parser);
37674 return;
37675 }
37676
37677 if (oacc_get_fn_attrib (fndecl))
37678 {
37679 error_at (parser->oacc_routine->loc,
37680 "%<#pragma acc routine%> already applied to %qD", fndecl);
37681 parser->oacc_routine = NULL;
37682 return;
37683 }
37684
37685 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37686 {
37687 error_at (parser->oacc_routine->loc,
37688 TREE_USED (fndecl)
37689 ? G_("%<#pragma acc routine%> must be applied before use")
37690 : G_("%<#pragma acc routine%> must be applied before "
37691 "definition"));
37692 parser->oacc_routine = NULL;
37693 return;
37694 }
37695
37696 /* Process the routine's dimension clauses. */
37697 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37698 oacc_replace_fn_attrib (fndecl, dims);
37699
37700 /* Add an "omp declare target" attribute. */
37701 DECL_ATTRIBUTES (fndecl)
37702 = tree_cons (get_identifier ("omp declare target"),
37703 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37704
37705 /* Don't unset parser->oacc_routine here: we may still need it to
37706 diagnose wrong usage. But, remember that we've used this "#pragma acc
37707 routine". */
37708 parser->oacc_routine->fndecl_seen = true;
37709 }
37710}
37711
37712/* Main entry point to OpenMP statement pragmas. */
37713
37714static void
37715cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37716{
37717 tree stmt;
37718 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37719 omp_clause_mask mask (0);
37720
37721 switch (cp_parser_pragma_kind (pragma_tok))
37722 {
37723 case PRAGMA_OACC_ATOMIC:
37724 cp_parser_omp_atomic (parser, pragma_tok);
37725 return;
37726 case PRAGMA_OACC_CACHE:
37727 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37728 break;
37729 case PRAGMA_OACC_DATA:
37730 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37731 break;
37732 case PRAGMA_OACC_ENTER_DATA:
37733 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37734 break;
37735 case PRAGMA_OACC_EXIT_DATA:
37736 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37737 break;
37738 case PRAGMA_OACC_HOST_DATA:
37739 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37740 break;
37741 case PRAGMA_OACC_KERNELS:
37742 case PRAGMA_OACC_PARALLEL:
37743 strcpy (p_name, "#pragma acc");
37744 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37745 if_p);
37746 break;
37747 case PRAGMA_OACC_LOOP:
37748 strcpy (p_name, "#pragma acc");
37749 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37750 if_p);
37751 break;
37752 case PRAGMA_OACC_UPDATE:
37753 stmt = cp_parser_oacc_update (parser, pragma_tok);
37754 break;
37755 case PRAGMA_OACC_WAIT:
37756 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37757 break;
37758 case PRAGMA_OMP_ATOMIC:
37759 cp_parser_omp_atomic (parser, pragma_tok);
37760 return;
37761 case PRAGMA_OMP_CRITICAL:
37762 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37763 break;
37764 case PRAGMA_OMP_DISTRIBUTE:
37765 strcpy (p_name, "#pragma omp");
37766 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37767 if_p);
37768 break;
37769 case PRAGMA_OMP_FOR:
37770 strcpy (p_name, "#pragma omp");
37771 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37772 if_p);
37773 break;
37774 case PRAGMA_OMP_MASTER:
37775 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37776 break;
37777 case PRAGMA_OMP_PARALLEL:
37778 strcpy (p_name, "#pragma omp");
37779 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37780 if_p);
37781 break;
37782 case PRAGMA_OMP_SECTIONS:
37783 strcpy (p_name, "#pragma omp");
37784 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37785 break;
37786 case PRAGMA_OMP_SIMD:
37787 strcpy (p_name, "#pragma omp");
37788 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37789 if_p);
37790 break;
37791 case PRAGMA_OMP_SINGLE:
37792 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37793 break;
37794 case PRAGMA_OMP_TASK:
37795 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37796 break;
37797 case PRAGMA_OMP_TASKGROUP:
37798 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37799 break;
37800 case PRAGMA_OMP_TASKLOOP:
37801 strcpy (p_name, "#pragma omp");
37802 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37803 if_p);
37804 break;
37805 case PRAGMA_OMP_TEAMS:
37806 strcpy (p_name, "#pragma omp");
37807 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37808 if_p);
37809 break;
37810 default:
37811 gcc_unreachable ();
37812 }
37813
37814 protected_set_expr_location (stmt, pragma_tok->location);
37815}
37816
37817/* Transactional Memory parsing routines. */
37818
37819/* Parse a transaction attribute.
37820
37821 txn-attribute:
37822 attribute
37823 [ [ identifier ] ]
37824
37825 We use this instead of cp_parser_attributes_opt for transactions to avoid
37826 the pedwarn in C++98 mode. */
37827
37828static tree
37829cp_parser_txn_attribute_opt (cp_parser *parser)
37830{
37831 cp_token *token;
37832 tree attr_name, attr = NULL;
37833
37834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37835 return cp_parser_attributes_opt (parser);
37836
37837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37838 return NULL_TREE;
37839 cp_lexer_consume_token (parser->lexer);
37840 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37841 goto error1;
37842
37843 token = cp_lexer_peek_token (parser->lexer);
37844 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37845 {
37846 token = cp_lexer_consume_token (parser->lexer);
37847
37848 attr_name = (token->type == CPP_KEYWORD
37849 /* For keywords, use the canonical spelling,
37850 not the parsed identifier. */
37851 ? ridpointers[(int) token->keyword]
37852 : token->u.value);
37853 attr = build_tree_list (attr_name, NULL_TREE);
37854 }
37855 else
37856 cp_parser_error (parser, "expected identifier");
37857
37858 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37859 error1:
37860 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37861 return attr;
37862}
37863
37864/* Parse a __transaction_atomic or __transaction_relaxed statement.
37865
37866 transaction-statement:
37867 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37868 compound-statement
37869 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37870*/
37871
37872static tree
37873cp_parser_transaction (cp_parser *parser, cp_token *token)
37874{
37875 unsigned char old_in = parser->in_transaction;
37876 unsigned char this_in = 1, new_in;
37877 enum rid keyword = token->keyword;
37878 tree stmt, attrs, noex;
37879
37880 cp_lexer_consume_token (parser->lexer);
37881
37882 if (keyword == RID_TRANSACTION_RELAXED
37883 || keyword == RID_SYNCHRONIZED)
37884 this_in |= TM_STMT_ATTR_RELAXED;
37885 else
37886 {
37887 attrs = cp_parser_txn_attribute_opt (parser);
37888 if (attrs)
37889 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37890 }
37891
37892 /* Parse a noexcept specification. */
37893 if (keyword == RID_ATOMIC_NOEXCEPT)
37894 noex = boolean_true_node;
37895 else if (keyword == RID_ATOMIC_CANCEL)
37896 {
37897 /* cancel-and-throw is unimplemented. */
37898 sorry ("atomic_cancel");
37899 noex = NULL_TREE;
37900 }
37901 else
37902 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37903
37904 /* Keep track if we're in the lexical scope of an outer transaction. */
37905 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37906
37907 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37908
37909 parser->in_transaction = new_in;
37910 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37911 parser->in_transaction = old_in;
37912
37913 finish_transaction_stmt (stmt, NULL, this_in, noex);
37914
37915 return stmt;
37916}
37917
37918/* Parse a __transaction_atomic or __transaction_relaxed expression.
37919
37920 transaction-expression:
37921 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37922 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37923*/
37924
37925static tree
37926cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37927{
37928 unsigned char old_in = parser->in_transaction;
37929 unsigned char this_in = 1;
37930 cp_token *token;
37931 tree expr, noex;
37932 bool noex_expr;
37933 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37934
37935 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37936 || keyword == RID_TRANSACTION_RELAXED);
37937
37938 if (!flag_tm)
37939 error_at (loc,
37940 keyword == RID_TRANSACTION_RELAXED
37941 ? G_("%<__transaction_relaxed%> without transactional memory "
37942 "support enabled")
37943 : G_("%<__transaction_atomic%> without transactional memory "
37944 "support enabled"));
37945
37946 token = cp_parser_require_keyword (parser, keyword,
37947 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37948 : RT_TRANSACTION_RELAXED));
37949 gcc_assert (token != NULL);
37950
37951 if (keyword == RID_TRANSACTION_RELAXED)
37952 this_in |= TM_STMT_ATTR_RELAXED;
37953
37954 /* Set this early. This might mean that we allow transaction_cancel in
37955 an expression that we find out later actually has to be a constexpr.
37956 However, we expect that cxx_constant_value will be able to deal with
37957 this; also, if the noexcept has no constexpr, then what we parse next
37958 really is a transaction's body. */
37959 parser->in_transaction = this_in;
37960
37961 /* Parse a noexcept specification. */
37962 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37963 true);
37964
37965 if (!noex || !noex_expr
37966 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37967 {
37968 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37969
37970 expr = cp_parser_expression (parser);
37971 expr = finish_parenthesized_expr (expr);
37972
37973 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37974 }
37975 else
37976 {
37977 /* The only expression that is available got parsed for the noexcept
37978 already. noexcept is true then. */
37979 expr = noex;
37980 noex = boolean_true_node;
37981 }
37982
37983 expr = build_transaction_expr (token->location, expr, this_in, noex);
37984 parser->in_transaction = old_in;
37985
37986 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37987 return error_mark_node;
37988
37989 return (flag_tm ? expr : error_mark_node);
37990}
37991
37992/* Parse a function-transaction-block.
37993
37994 function-transaction-block:
37995 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37996 function-body
37997 __transaction_atomic txn-attribute[opt] function-try-block
37998 __transaction_relaxed ctor-initializer[opt] function-body
37999 __transaction_relaxed function-try-block
38000*/
38001
38002static bool
38003cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38004{
38005 unsigned char old_in = parser->in_transaction;
38006 unsigned char new_in = 1;
38007 tree compound_stmt, stmt, attrs;
38008 bool ctor_initializer_p;
38009 cp_token *token;
38010
38011 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38012 || keyword == RID_TRANSACTION_RELAXED);
38013 token = cp_parser_require_keyword (parser, keyword,
38014 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38015 : RT_TRANSACTION_RELAXED));
38016 gcc_assert (token != NULL);
38017
38018 if (keyword == RID_TRANSACTION_RELAXED)
38019 new_in |= TM_STMT_ATTR_RELAXED;
38020 else
38021 {
38022 attrs = cp_parser_txn_attribute_opt (parser);
38023 if (attrs)
38024 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38025 }
38026
38027 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38028
38029 parser->in_transaction = new_in;
38030
38031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38032 ctor_initializer_p = cp_parser_function_try_block (parser);
38033 else
38034 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38035 (parser, /*in_function_try_block=*/false);
38036
38037 parser->in_transaction = old_in;
38038
38039 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38040
38041 return ctor_initializer_p;
38042}
38043
38044/* Parse a __transaction_cancel statement.
38045
38046 cancel-statement:
38047 __transaction_cancel txn-attribute[opt] ;
38048 __transaction_cancel txn-attribute[opt] throw-expression ;
38049
38050 ??? Cancel and throw is not yet implemented. */
38051
38052static tree
38053cp_parser_transaction_cancel (cp_parser *parser)
38054{
38055 cp_token *token;
38056 bool is_outer = false;
38057 tree stmt, attrs;
38058
38059 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38060 RT_TRANSACTION_CANCEL);
38061 gcc_assert (token != NULL);
38062
38063 attrs = cp_parser_txn_attribute_opt (parser);
38064 if (attrs)
38065 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38066
38067 /* ??? Parse cancel-and-throw here. */
38068
38069 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38070
38071 if (!flag_tm)
38072 {
38073 error_at (token->location, "%<__transaction_cancel%> without "
38074 "transactional memory support enabled");
38075 return error_mark_node;
38076 }
38077 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38078 {
38079 error_at (token->location, "%<__transaction_cancel%> within a "
38080 "%<__transaction_relaxed%>");
38081 return error_mark_node;
38082 }
38083 else if (is_outer)
38084 {
38085 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38086 && !is_tm_may_cancel_outer (current_function_decl))
38087 {
38088 error_at (token->location, "outer %<__transaction_cancel%> not "
38089 "within outer %<__transaction_atomic%>");
38090 error_at (token->location,
38091 " or a %<transaction_may_cancel_outer%> function");
38092 return error_mark_node;
38093 }
38094 }
38095 else if (parser->in_transaction == 0)
38096 {
38097 error_at (token->location, "%<__transaction_cancel%> not within "
38098 "%<__transaction_atomic%>");
38099 return error_mark_node;
38100 }
38101
38102 stmt = build_tm_abort_call (token->location, is_outer);
38103 add_stmt (stmt);
38104
38105 return stmt;
38106}
38107
38108/* The parser. */
38109
38110static GTY (()) cp_parser *the_parser;
38111
38112
38113/* Special handling for the first token or line in the file. The first
38114 thing in the file might be #pragma GCC pch_preprocess, which loads a
38115 PCH file, which is a GC collection point. So we need to handle this
38116 first pragma without benefit of an existing lexer structure.
38117
38118 Always returns one token to the caller in *FIRST_TOKEN. This is
38119 either the true first token of the file, or the first token after
38120 the initial pragma. */
38121
38122static void
38123cp_parser_initial_pragma (cp_token *first_token)
38124{
38125 tree name = NULL;
38126
38127 cp_lexer_get_preprocessor_token (NULL, first_token);
38128 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38129 return;
38130
38131 cp_lexer_get_preprocessor_token (NULL, first_token);
38132 if (first_token->type == CPP_STRING)
38133 {
38134 name = first_token->u.value;
38135
38136 cp_lexer_get_preprocessor_token (NULL, first_token);
38137 if (first_token->type != CPP_PRAGMA_EOL)
38138 error_at (first_token->location,
38139 "junk at end of %<#pragma GCC pch_preprocess%>");
38140 }
38141 else
38142 error_at (first_token->location, "expected string literal");
38143
38144 /* Skip to the end of the pragma. */
38145 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38146 cp_lexer_get_preprocessor_token (NULL, first_token);
38147
38148 /* Now actually load the PCH file. */
38149 if (name)
38150 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38151
38152 /* Read one more token to return to our caller. We have to do this
38153 after reading the PCH file in, since its pointers have to be
38154 live. */
38155 cp_lexer_get_preprocessor_token (NULL, first_token);
38156}
38157
38158/* Parses the grainsize pragma for the _Cilk_for statement.
38159 Syntax:
38160 #pragma cilk grainsize = <VALUE>. */
38161
38162static void
38163cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38164{
38165 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38166 {
38167 tree exp = cp_parser_binary_expression (parser, false, false,
38168 PREC_NOT_OPERATOR, NULL);
38169 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38170 if (!exp || exp == error_mark_node)
38171 {
38172 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38173 return;
38174 }
38175
38176 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38178 cp_parser_cilk_for (parser, exp, if_p);
38179 else
38180 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38181 "%<#pragma cilk grainsize%> is not followed by "
38182 "%<_Cilk_for%>");
38183 return;
38184 }
38185 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38186}
38187
38188/* Normal parsing of a pragma token. Here we can (and must) use the
38189 regular lexer. */
38190
38191static bool
38192cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38193{
38194 cp_token *pragma_tok;
38195 unsigned int id;
38196 tree stmt;
38197 bool ret;
38198
38199 pragma_tok = cp_lexer_consume_token (parser->lexer);
38200 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38201 parser->lexer->in_pragma = true;
38202
38203 id = cp_parser_pragma_kind (pragma_tok);
38204 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38205 cp_ensure_no_omp_declare_simd (parser);
38206 switch (id)
38207 {
38208 case PRAGMA_GCC_PCH_PREPROCESS:
38209 error_at (pragma_tok->location,
38210 "%<#pragma GCC pch_preprocess%> must be first");
38211 break;
38212
38213 case PRAGMA_OMP_BARRIER:
38214 switch (context)
38215 {
38216 case pragma_compound:
38217 cp_parser_omp_barrier (parser, pragma_tok);
38218 return false;
38219 case pragma_stmt:
38220 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38221 "used in compound statements", "omp barrier");
38222 break;
38223 default:
38224 goto bad_stmt;
38225 }
38226 break;
38227
38228 case PRAGMA_OMP_FLUSH:
38229 switch (context)
38230 {
38231 case pragma_compound:
38232 cp_parser_omp_flush (parser, pragma_tok);
38233 return false;
38234 case pragma_stmt:
38235 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38236 "used in compound statements", "omp flush");
38237 break;
38238 default:
38239 goto bad_stmt;
38240 }
38241 break;
38242
38243 case PRAGMA_OMP_TASKWAIT:
38244 switch (context)
38245 {
38246 case pragma_compound:
38247 cp_parser_omp_taskwait (parser, pragma_tok);
38248 return false;
38249 case pragma_stmt:
38250 error_at (pragma_tok->location,
38251 "%<#pragma %s%> may only be used in compound statements",
38252 "omp taskwait");
38253 break;
38254 default:
38255 goto bad_stmt;
38256 }
38257 break;
38258
38259 case PRAGMA_OMP_TASKYIELD:
38260 switch (context)
38261 {
38262 case pragma_compound:
38263 cp_parser_omp_taskyield (parser, pragma_tok);
38264 return false;
38265 case pragma_stmt:
38266 error_at (pragma_tok->location,
38267 "%<#pragma %s%> may only be used in compound statements",
38268 "omp taskyield");
38269 break;
38270 default:
38271 goto bad_stmt;
38272 }
38273 break;
38274
38275 case PRAGMA_OMP_CANCEL:
38276 switch (context)
38277 {
38278 case pragma_compound:
38279 cp_parser_omp_cancel (parser, pragma_tok);
38280 return false;
38281 case pragma_stmt:
38282 error_at (pragma_tok->location,
38283 "%<#pragma %s%> may only be used in compound statements",
38284 "omp cancel");
38285 break;
38286 default:
38287 goto bad_stmt;
38288 }
38289 break;
38290
38291 case PRAGMA_OMP_CANCELLATION_POINT:
38292 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38293 return false;
38294
38295 case PRAGMA_OMP_THREADPRIVATE:
38296 cp_parser_omp_threadprivate (parser, pragma_tok);
38297 return false;
38298
38299 case PRAGMA_OMP_DECLARE:
38300 return cp_parser_omp_declare (parser, pragma_tok, context);
38301
38302 case PRAGMA_OACC_DECLARE:
38303 cp_parser_oacc_declare (parser, pragma_tok);
38304 return false;
38305
38306 case PRAGMA_OACC_ENTER_DATA:
38307 if (context == pragma_stmt)
38308 {
38309 error_at (pragma_tok->location,
38310 "%<#pragma %s%> may only be used in compound statements",
38311 "acc enter data");
38312 break;
38313 }
38314 else if (context != pragma_compound)
38315 goto bad_stmt;
38316 cp_parser_omp_construct (parser, pragma_tok, if_p);
38317 return true;
38318
38319 case PRAGMA_OACC_EXIT_DATA:
38320 if (context == pragma_stmt)
38321 {
38322 error_at (pragma_tok->location,
38323 "%<#pragma %s%> may only be used in compound statements",
38324 "acc exit data");
38325 break;
38326 }
38327 else if (context != pragma_compound)
38328 goto bad_stmt;
38329 cp_parser_omp_construct (parser, pragma_tok, if_p);
38330 return true;
38331
38332 case PRAGMA_OACC_ROUTINE:
38333 if (context != pragma_external)
38334 {
38335 error_at (pragma_tok->location,
38336 "%<#pragma acc routine%> must be at file scope");
38337 break;
38338 }
38339 cp_parser_oacc_routine (parser, pragma_tok, context);
38340 return false;
38341
38342 case PRAGMA_OACC_UPDATE:
38343 if (context == pragma_stmt)
38344 {
38345 error_at (pragma_tok->location,
38346 "%<#pragma %s%> may only be used in compound statements",
38347 "acc update");
38348 break;
38349 }
38350 else if (context != pragma_compound)
38351 goto bad_stmt;
38352 cp_parser_omp_construct (parser, pragma_tok, if_p);
38353 return true;
38354
38355 case PRAGMA_OACC_WAIT:
38356 if (context == pragma_stmt)
38357 {
38358 error_at (pragma_tok->location,
38359 "%<#pragma %s%> may only be used in compound statements",
38360 "acc wait");
38361 break;
38362 }
38363 else if (context != pragma_compound)
38364 goto bad_stmt;
38365 cp_parser_omp_construct (parser, pragma_tok, if_p);
38366 return true;
38367
38368 case PRAGMA_OACC_ATOMIC:
38369 case PRAGMA_OACC_CACHE:
38370 case PRAGMA_OACC_DATA:
38371 case PRAGMA_OACC_HOST_DATA:
38372 case PRAGMA_OACC_KERNELS:
38373 case PRAGMA_OACC_PARALLEL:
38374 case PRAGMA_OACC_LOOP:
38375 case PRAGMA_OMP_ATOMIC:
38376 case PRAGMA_OMP_CRITICAL:
38377 case PRAGMA_OMP_DISTRIBUTE:
38378 case PRAGMA_OMP_FOR:
38379 case PRAGMA_OMP_MASTER:
38380 case PRAGMA_OMP_PARALLEL:
38381 case PRAGMA_OMP_SECTIONS:
38382 case PRAGMA_OMP_SIMD:
38383 case PRAGMA_OMP_SINGLE:
38384 case PRAGMA_OMP_TASK:
38385 case PRAGMA_OMP_TASKGROUP:
38386 case PRAGMA_OMP_TASKLOOP:
38387 case PRAGMA_OMP_TEAMS:
38388 if (context != pragma_stmt && context != pragma_compound)
38389 goto bad_stmt;
38390 stmt = push_omp_privatization_clauses (false);
38391 cp_parser_omp_construct (parser, pragma_tok, if_p);
38392 pop_omp_privatization_clauses (stmt);
38393 return true;
38394
38395 case PRAGMA_OMP_ORDERED:
38396 if (context != pragma_stmt && context != pragma_compound)
38397 goto bad_stmt;
38398 stmt = push_omp_privatization_clauses (false);
38399 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38400 pop_omp_privatization_clauses (stmt);
38401 return ret;
38402
38403 case PRAGMA_OMP_TARGET:
38404 if (context != pragma_stmt && context != pragma_compound)
38405 goto bad_stmt;
38406 stmt = push_omp_privatization_clauses (false);
38407 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38408 pop_omp_privatization_clauses (stmt);
38409 return ret;
38410
38411 case PRAGMA_OMP_END_DECLARE_TARGET:
38412 cp_parser_omp_end_declare_target (parser, pragma_tok);
38413 return false;
38414
38415 case PRAGMA_OMP_SECTION:
38416 error_at (pragma_tok->location,
38417 "%<#pragma omp section%> may only be used in "
38418 "%<#pragma omp sections%> construct");
38419 break;
38420
38421 case PRAGMA_IVDEP:
38422 {
38423 if (context == pragma_external)
38424 {
38425 error_at (pragma_tok->location,
38426 "%<#pragma GCC ivdep%> must be inside a function");
38427 break;
38428 }
38429 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38430 cp_token *tok;
38431 tok = cp_lexer_peek_token (the_parser->lexer);
38432 if (tok->type != CPP_KEYWORD
38433 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38434 && tok->keyword != RID_DO))
38435 {
38436 cp_parser_error (parser, "for, while or do statement expected");
38437 return false;
38438 }
38439 cp_parser_iteration_statement (parser, if_p, true);
38440 return true;
38441 }
38442
38443 case PRAGMA_CILK_SIMD:
38444 if (context == pragma_external)
38445 {
38446 error_at (pragma_tok->location,
38447 "%<#pragma simd%> must be inside a function");
38448 break;
38449 }
38450 stmt = push_omp_privatization_clauses (false);
38451 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38452 pop_omp_privatization_clauses (stmt);
38453 return true;
38454
38455 case PRAGMA_CILK_GRAINSIZE:
38456 if (context == pragma_external)
38457 {
38458 error_at (pragma_tok->location,
38459 "%<#pragma cilk grainsize%> must be inside a function");
38460 break;
38461 }
38462
38463 /* Ignore the pragma if Cilk Plus is not enabled. */
38464 if (flag_cilkplus)
38465 {
38466 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38467 return true;
38468 }
38469 else
38470 {
38471 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38472 "%<#pragma cilk grainsize%>");
38473 break;
38474 }
38475
38476 default:
38477 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38478 c_invoke_pragma_handler (id);
38479 break;
38480
38481 bad_stmt:
38482 cp_parser_error (parser, "expected declaration specifiers");
38483 break;
38484 }
38485
38486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38487 return false;
38488}
38489
38490/* The interface the pragma parsers have to the lexer. */
38491
38492enum cpp_ttype
38493pragma_lex (tree *value, location_t *loc)
38494{
38495 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38496 enum cpp_ttype ret = tok->type;
38497
38498 *value = tok->u.value;
38499 if (loc)
38500 *loc = tok->location;
38501
38502 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38503 ret = CPP_EOF;
38504 else if (ret == CPP_STRING)
38505 *value = cp_parser_string_literal (the_parser, false, false);
38506 else
38507 {
38508 if (ret == CPP_KEYWORD)
38509 ret = CPP_NAME;
38510 cp_lexer_consume_token (the_parser->lexer);
38511 }
38512
38513 return ret;
38514}
38515
38516
38517/* External interface. */
38518
38519/* Parse one entire translation unit. */
38520
38521void
38522c_parse_file (void)
38523{
38524 static bool already_called = false;
38525
38526 if (already_called)
38527 fatal_error (input_location,
38528 "inter-module optimizations not implemented for C++");
38529 already_called = true;
38530
38531 the_parser = cp_parser_new ();
38532 push_deferring_access_checks (flag_access_control
38533 ? dk_no_deferred : dk_no_check);
38534 cp_parser_translation_unit (the_parser);
38535 the_parser = NULL;
38536}
38537
38538/* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38539 vectorlength clause:
38540 Syntax:
38541 vectorlength ( constant-expression ) */
38542
38543static tree
38544cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38545 bool is_simd_fn)
38546{
38547 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38548 tree expr;
38549 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38550 safelen clause. Thus, vectorlength is represented as OMP 4.0
38551 safelen. For SIMD-enabled function it is represented by OMP 4.0
38552 simdlen. */
38553 if (!is_simd_fn)
38554 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38555 loc);
38556 else
38557 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38558 loc);
38559
38560 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38561 return error_mark_node;
38562
38563 expr = cp_parser_constant_expression (parser);
38564 expr = maybe_constant_value (expr);
38565
38566 /* If expr == error_mark_node, then don't emit any errors nor
38567 create a clause. if any of the above functions returns
38568 error mark node then they would have emitted an error message. */
38569 if (expr == error_mark_node)
38570 ;
38571 else if (!TREE_TYPE (expr)
38572 || !TREE_CONSTANT (expr)
38573 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38574 error_at (loc, "vectorlength must be an integer constant");
38575 else if (TREE_CONSTANT (expr)
38576 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38577 error_at (loc, "vectorlength must be a power of 2");
38578 else
38579 {
38580 tree c;
38581 if (!is_simd_fn)
38582 {
38583 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38584 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38585 OMP_CLAUSE_CHAIN (c) = clauses;
38586 clauses = c;
38587 }
38588 else
38589 {
38590 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38591 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38592 OMP_CLAUSE_CHAIN (c) = clauses;
38593 clauses = c;
38594 }
38595 }
38596
38597 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38598 return error_mark_node;
38599 return clauses;
38600}
38601
38602/* Handles the Cilk Plus #pragma simd linear clause.
38603 Syntax:
38604 linear ( simd-linear-variable-list )
38605
38606 simd-linear-variable-list:
38607 simd-linear-variable
38608 simd-linear-variable-list , simd-linear-variable
38609
38610 simd-linear-variable:
38611 id-expression
38612 id-expression : simd-linear-step
38613
38614 simd-linear-step:
38615 conditional-expression */
38616
38617static tree
38618cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38619{
38620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38621
38622 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38623 return clauses;
38624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38625 {
38626 cp_parser_error (parser, "expected identifier");
38627 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38628 return error_mark_node;
38629 }
38630
38631 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38632 parser->colon_corrects_to_scope_p = false;
38633 while (1)
38634 {
38635 cp_token *token = cp_lexer_peek_token (parser->lexer);
38636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38637 {
38638 cp_parser_error (parser, "expected variable-name");
38639 clauses = error_mark_node;
38640 break;
38641 }
38642
38643 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38644 false, false);
38645 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38646 token->location);
38647 if (decl == error_mark_node)
38648 {
38649 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38650 token->location);
38651 clauses = error_mark_node;
38652 }
38653 else
38654 {
38655 tree e = NULL_TREE;
38656 tree step_size = integer_one_node;
38657
38658 /* If present, parse the linear step. Otherwise, assume the default
38659 value of 1. */
38660 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38661 {
38662 cp_lexer_consume_token (parser->lexer);
38663
38664 e = cp_parser_assignment_expression (parser);
38665 e = maybe_constant_value (e);
38666
38667 if (e == error_mark_node)
38668 {
38669 /* If an error has occurred, then the whole pragma is
38670 considered ill-formed. Thus, no reason to keep
38671 parsing. */
38672 clauses = error_mark_node;
38673 break;
38674 }
38675 else if (type_dependent_expression_p (e)
38676 || value_dependent_expression_p (e)
38677 || (TREE_TYPE (e)
38678 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38679 && (TREE_CONSTANT (e)
38680 || DECL_P (e))))
38681 step_size = e;
38682 else
38683 cp_parser_error (parser,
38684 "step size must be an integer constant "
38685 "expression or an integer variable");
38686 }
38687
38688 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38689 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38690 OMP_CLAUSE_DECL (l) = decl;
38691 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38692 OMP_CLAUSE_CHAIN (l) = clauses;
38693 clauses = l;
38694 }
38695 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38696 cp_lexer_consume_token (parser->lexer);
38697 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38698 break;
38699 else
38700 {
38701 error_at (cp_lexer_peek_token (parser->lexer)->location,
38702 "expected %<,%> or %<)%> after %qE", decl);
38703 clauses = error_mark_node;
38704 break;
38705 }
38706 }
38707 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38708 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38709 return clauses;
38710}
38711
38712/* Returns the name of the next clause. If the clause is not
38713 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38714 token is not consumed. Otherwise, the appropriate enum from the
38715 pragma_simd_clause is returned and the token is consumed. */
38716
38717static pragma_omp_clause
38718cp_parser_cilk_simd_clause_name (cp_parser *parser)
38719{
38720 pragma_omp_clause clause_type;
38721 cp_token *token = cp_lexer_peek_token (parser->lexer);
38722
38723 if (token->keyword == RID_PRIVATE)
38724 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38725 else if (!token->u.value || token->type != CPP_NAME)
38726 return PRAGMA_CILK_CLAUSE_NONE;
38727 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38728 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38729 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38730 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38731 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38732 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38733 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38734 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38735 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38736 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38737 else
38738 return PRAGMA_CILK_CLAUSE_NONE;
38739
38740 cp_lexer_consume_token (parser->lexer);
38741 return clause_type;
38742}
38743
38744/* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38745
38746static tree
38747cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38748{
38749 tree clauses = NULL_TREE;
38750
38751 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38752 && clauses != error_mark_node)
38753 {
38754 pragma_omp_clause c_kind;
38755 c_kind = cp_parser_cilk_simd_clause_name (parser);
38756 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38757 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38758 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38759 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38760 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38761 /* Use the OpenMP 4.0 equivalent function. */
38762 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38763 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38764 /* Use the OpenMP 4.0 equivalent function. */
38765 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38766 clauses);
38767 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38768 /* Use the OMP 4.0 equivalent function. */
38769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38770 clauses);
38771 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38772 /* Use the OMP 4.0 equivalent function. */
38773 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38774 else
38775 {
38776 clauses = error_mark_node;
38777 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38778 break;
38779 }
38780 }
38781
38782 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38783
38784 if (clauses == error_mark_node)
38785 return error_mark_node;
38786 else
38787 return finish_omp_clauses (clauses, C_ORT_CILK);
38788}
38789
38790/* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38791
38792static void
38793cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38794{
38795 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38796
38797 if (clauses == error_mark_node)
38798 return;
38799
38800 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38801 {
38802 error_at (cp_lexer_peek_token (parser->lexer)->location,
38803 "for statement expected");
38804 return;
38805 }
38806
38807 tree sb = begin_omp_structured_block ();
38808 int save = cp_parser_begin_omp_structured_block (parser);
38809 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38810 if (ret)
38811 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38812 cp_parser_end_omp_structured_block (parser, save);
38813 add_stmt (finish_omp_structured_block (sb));
38814}
38815
38816/* Main entry-point for parsing Cilk Plus _Cilk_for
38817 loops. The return value is error_mark_node
38818 when errors happen and CILK_FOR tree on success. */
38819
38820static tree
38821cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38822{
38823 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38824 gcc_unreachable ();
38825
38826 tree sb = begin_omp_structured_block ();
38827 int save = cp_parser_begin_omp_structured_block (parser);
38828
38829 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38830 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38831 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38832 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38833
38834 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38835 if (ret)
38836 cpp_validate_cilk_plus_loop (ret);
38837 else
38838 ret = error_mark_node;
38839
38840 cp_parser_end_omp_structured_block (parser, save);
38841 add_stmt (finish_omp_structured_block (sb));
38842 return ret;
38843}
38844
38845/* Create an identifier for a generic parameter type (a synthesized
38846 template parameter implied by `auto' or a concept identifier). */
38847
38848static GTY(()) int generic_parm_count;
38849static tree
38850make_generic_type_name ()
38851{
38852 char buf[32];
38853 sprintf (buf, "auto:%d", ++generic_parm_count);
38854 return get_identifier (buf);
38855}
38856
38857/* Predicate that behaves as is_auto_or_concept but matches the parent
38858 node of the generic type rather than the generic type itself. This
38859 allows for type transformation in add_implicit_template_parms. */
38860
38861static inline bool
38862tree_type_is_auto_or_concept (const_tree t)
38863{
38864 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38865}
38866
38867/* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38868 (creating a new template parameter list if necessary). Returns the newly
38869 created template type parm. */
38870
38871static tree
38872synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38873{
38874 gcc_assert (current_binding_level->kind == sk_function_parms);
38875
38876 /* Before committing to modifying any scope, if we're in an
38877 implicit template scope, and we're trying to synthesize a
38878 constrained parameter, try to find a previous parameter with
38879 the same name. This is the same-type rule for abbreviated
38880 function templates.
38881
38882 NOTE: We can generate implicit parameters when tentatively
38883 parsing a nested name specifier, only to reject that parse
38884 later. However, matching the same template-id as part of a
38885 direct-declarator should generate an identical template
38886 parameter, so this rule will merge them. */
38887 if (parser->implicit_template_scope && constr)
38888 {
38889 tree t = parser->implicit_template_parms;
38890 while (t)
38891 {
38892 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38893 {
38894 tree d = TREE_VALUE (t);
38895 if (TREE_CODE (d) == PARM_DECL)
38896 /* Return the TEMPLATE_PARM_INDEX. */
38897 d = DECL_INITIAL (d);
38898 return d;
38899 }
38900 t = TREE_CHAIN (t);
38901 }
38902 }
38903
38904 /* We are either continuing a function template that already contains implicit
38905 template parameters, creating a new fully-implicit function template, or
38906 extending an existing explicit function template with implicit template
38907 parameters. */
38908
38909 cp_binding_level *const entry_scope = current_binding_level;
38910
38911 bool become_template = false;
38912 cp_binding_level *parent_scope = 0;
38913
38914 if (parser->implicit_template_scope)
38915 {
38916 gcc_assert (parser->implicit_template_parms);
38917
38918 current_binding_level = parser->implicit_template_scope;
38919 }
38920 else
38921 {
38922 /* Roll back to the existing template parameter scope (in the case of
38923 extending an explicit function template) or introduce a new template
38924 parameter scope ahead of the function parameter scope (or class scope
38925 in the case of out-of-line member definitions). The function scope is
38926 added back after template parameter synthesis below. */
38927
38928 cp_binding_level *scope = entry_scope;
38929
38930 while (scope->kind == sk_function_parms)
38931 {
38932 parent_scope = scope;
38933 scope = scope->level_chain;
38934 }
38935 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38936 {
38937 /* If not defining a class, then any class scope is a scope level in
38938 an out-of-line member definition. In this case simply wind back
38939 beyond the first such scope to inject the template parameter list.
38940 Otherwise wind back to the class being defined. The latter can
38941 occur in class member friend declarations such as:
38942
38943 class A {
38944 void foo (auto);
38945 };
38946 class B {
38947 friend void A::foo (auto);
38948 };
38949
38950 The template parameter list synthesized for the friend declaration
38951 must be injected in the scope of 'B'. This can also occur in
38952 erroneous cases such as:
38953
38954 struct A {
38955 struct B {
38956 void foo (auto);
38957 };
38958 void B::foo (auto) {}
38959 };
38960
38961 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38962 but, nevertheless, the template parameter list synthesized for the
38963 declarator should be injected into the scope of 'A' as if the
38964 ill-formed template was specified explicitly. */
38965
38966 while (scope->kind == sk_class && !scope->defining_class_p)
38967 {
38968 parent_scope = scope;
38969 scope = scope->level_chain;
38970 }
38971 }
38972
38973 current_binding_level = scope;
38974
38975 if (scope->kind != sk_template_parms
38976 || !function_being_declared_is_template_p (parser))
38977 {
38978 /* Introduce a new template parameter list for implicit template
38979 parameters. */
38980
38981 become_template = true;
38982
38983 parser->implicit_template_scope
38984 = begin_scope (sk_template_parms, NULL);
38985
38986 ++processing_template_decl;
38987
38988 parser->fully_implicit_function_template_p = true;
38989 ++parser->num_template_parameter_lists;
38990 }
38991 else
38992 {
38993 /* Synthesize implicit template parameters at the end of the explicit
38994 template parameter list. */
38995
38996 gcc_assert (current_template_parms);
38997
38998 parser->implicit_template_scope = scope;
38999
39000 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39001 parser->implicit_template_parms
39002 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39003 }
39004 }
39005
39006 /* Synthesize a new template parameter and track the current template
39007 parameter chain with implicit_template_parms. */
39008
39009 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39010 tree synth_id = make_generic_type_name ();
39011 tree synth_tmpl_parm;
39012 bool non_type = false;
39013
39014 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39015 synth_tmpl_parm
39016 = finish_template_type_parm (class_type_node, synth_id);
39017 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39018 synth_tmpl_parm
39019 = finish_constrained_template_template_parm (proto, synth_id);
39020 else
39021 {
39022 synth_tmpl_parm = copy_decl (proto);
39023 DECL_NAME (synth_tmpl_parm) = synth_id;
39024 non_type = true;
39025 }
39026
39027 // Attach the constraint to the parm before processing.
39028 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39029 TREE_TYPE (node) = constr;
39030 tree new_parm
39031 = process_template_parm (parser->implicit_template_parms,
39032 input_location,
39033 node,
39034 /*non_type=*/non_type,
39035 /*param_pack=*/false);
39036
39037 // Chain the new parameter to the list of implicit parameters.
39038 if (parser->implicit_template_parms)
39039 parser->implicit_template_parms
39040 = TREE_CHAIN (parser->implicit_template_parms);
39041 else
39042 parser->implicit_template_parms = new_parm;
39043
39044 tree new_decl = getdecls ();
39045 if (non_type)
39046 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39047 new_decl = DECL_INITIAL (new_decl);
39048
39049 /* If creating a fully implicit function template, start the new implicit
39050 template parameter list with this synthesized type, otherwise grow the
39051 current template parameter list. */
39052
39053 if (become_template)
39054 {
39055 parent_scope->level_chain = current_binding_level;
39056
39057 tree new_parms = make_tree_vec (1);
39058 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39059 current_template_parms = tree_cons (size_int (processing_template_decl),
39060 new_parms, current_template_parms);
39061 }
39062 else
39063 {
39064 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39065 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39066 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39067 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39068 }
39069
39070 // If the new parameter was constrained, we need to add that to the
39071 // constraints in the template parameter list.
39072 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39073 {
39074 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39075 reqs = conjoin_constraints (reqs, req);
39076 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39077 }
39078
39079 current_binding_level = entry_scope;
39080
39081 return new_decl;
39082}
39083
39084/* Finish the declaration of a fully implicit function template. Such a
39085 template has no explicit template parameter list so has not been through the
39086 normal template head and tail processing. synthesize_implicit_template_parm
39087 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39088 provided if the declaration is a class member such that its template
39089 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39090 form is returned. Otherwise NULL_TREE is returned. */
39091
39092static tree
39093finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39094{
39095 gcc_assert (parser->fully_implicit_function_template_p);
39096
39097 if (member_decl_opt && member_decl_opt != error_mark_node
39098 && DECL_VIRTUAL_P (member_decl_opt))
39099 {
39100 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39101 "implicit templates may not be %<virtual%>");
39102 DECL_VIRTUAL_P (member_decl_opt) = false;
39103 }
39104
39105 if (member_decl_opt)
39106 member_decl_opt = finish_member_template_decl (member_decl_opt);
39107 end_template_decl ();
39108
39109 parser->fully_implicit_function_template_p = false;
39110 --parser->num_template_parameter_lists;
39111
39112 return member_decl_opt;
39113}
39114
39115#include "gt-cp-parser.h"
39116